Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hmm, I actually I'm not sure if it makes sense to answer my own question ...</p> <p>But due to the fact that I received no answer, mayby my initial question was a little bit stupid.</p> <p>Nevertheless, in the last day I spent some time to solve my issue. Basically my solution is to add additional segements according to the duration of the event. I spare you my calculations for the duration. This is because my initial interest was in how to provide a gradient to a polygon.</p> <p>Maybe some of you find my solution useful</p> <p>Cheers Tom</p> <pre><code>library(ggplot2) library(reshape) event.day &lt;- c("A", "A", "A", "A", "B", "B") event &lt;- c(1, 2, 3, 4, 5, 6) sys &lt;- c(120, 160, 100, 180, 100, 180) duration &lt;- c(50, 100, 50, 150, 350, 0) df &lt;- data.frame(event.day, event, sys, duration) df$end &lt;- c(df$sys[-1], NA) ## replacing na values df.value.na &lt;- is.na(df$end) df[df.value.na,]$end &lt;- df[df.value.na,]$sys ## calculating the slope df$slope &lt;- df$end / df$sys ## creating rows for each event depending on the duration event.id &lt;- vector() segment.id &lt;- vector() for(i in 1:nrow(df)) { event.id &lt;- c(event.id, rep(df[i,]$event, each = df[i,]$duration)) segment.id &lt;- c(segment.id,c(1:df[i,]$duration)) } ## merging the original dataframe with the additional segments df.segments &lt;- data.frame(event.id, segment.id) df &lt;- merge(df, df.segments, by.x = c("event"), by.y = c("event.id")) ## calculate the start and end values for the newly created segements df$segment.start &lt;- df$sys + (df$segment.id - 1) * (df$end - df$sys) / df$duration df$segment.end &lt;- df$sys + (df$segment.id) * (df$end - df$sys) / df$duration ## just a simple calculation value.max &lt;- max(df$sys) df$high &lt;- 1 + 0.45 * df$segment.end / value.max df$low &lt;- 1 - 0.45 * df$segment.end / value.max df$percent &lt;- df$segment.end / value.max df$id &lt;- seq_along(df$sys) df$idByDay &lt;- ave( 1:nrow(df), df$event.day,FUN=function(x) seq_along(x)) ## how many events in total, necessary newevents &lt;- nrow(df) ## subsetting the original data.frame df &lt;- df[,c("event.day", "id", "idByDay", "segment.id", "segment.start", "duration", "segment.end", "high", "low", "percent")] ## melting the data.frame df.melted &lt;- melt(df, id.vars = c("event.day", "id", "idByDay", "segment.id", "segment.start", "duration", "segment.end","percent")) df.melted &lt;- df.melted[order(df.melted$id,df.melted$segment.id),] ## this is a tricky one, basically this a self join, of two tables # every event is available twice, this is due to melt in the previous section # a dataframe is produced where every event is contained 4 times, except the first and last 2 rows, # the first row marks the start of the first polygon # the last row marks the end of the last polygon df.melted &lt;- rbind(df.melted[1:(nrow(df.melted)-2),],df.melted[3:nrow(df.melted),]) df.melted &lt;- df.melted[order(df.melted$id,df.melted$segment.id),] ## grouping, necessary for drawing the polygons # the 1st polygon spans from the 1st event, and the first 2 rows from 2nd event # the 2nd polygon spans from last 2 rows of the 2nd event and the first 2 rows from 3rd event # ... # the last polygon spans from the last 2 rows of the next to last event and the 2 rows of the last event df.melted$grouping &lt;- rep (1:(newevents-1), each=4) df.melted &lt;- df.melted[order(df.melted$id, df.melted$grouping, df.melted$variable), ] ## adding a 4 point for each group df.melted$point &lt;- rep(c(1,2,4,3),(newevents-1)) df.melted &lt;- df.melted[order(df.melted$grouping,df.melted$point), ] ## drawing the polygons p &lt;- ggplot() p &lt;- p + geom_polygon(data = df.melted ,aes( x = value ,y =idByDay ,group = grouping ,fill = percent ) ) p &lt;- p + labs(x = "something", y="something else") p &lt;- p + theme( panel.background = element_blank() #,panel.grid.minor = element_blank() #axis.title.x=element_blank() #,axis.text.x=element_text(size=12, face=2, color="darkgrey") #,axis.title.y=element_blank() #,axis.ticks.y = element_blank() #,axis.text.y = element_blank() ) p &lt;- p + scale_fill_gradient( low = "lightgrey" ,high = "red" ,guide = guide_legend( title = "Sys" ,order = 1 ,reverse = FALSE ,ncol = 2 ,override.aes = list(alpha = NA) ) ) p &lt;- p + facet_wrap(~event.day, ncol=2) p </code></pre> <p>Using this code I was able to create a chart that look like this:</p> <p><img src="https://i.stack.imgur.com/WcCSE.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload