1

I am currently struggeling with the use of ggplot. I have the following data:

num <- c(5,5,5,5,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8)
name <- c('4;1','4;1','4;1','4;1','4;2','4;2','4;2','3;3','4;2','3;3','4;2','3;3','4;2','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','3;4','4;3','4;4','4;4','4;4','4;4','4;4','4;4','4;4','4;4')
x <- c(1.7,1.8,1.9,2.0,1.5,1.6,1.7,1.8,1.8,1.9,1.9,2.0,2.0,1.4,1.5,1.5,1.6,1.6,1.7,1.7,1.8,1.8,1.9,1.9,2.0,2.0,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0)
min <- c(1.0,0.5,0.3,1.4,1.2,0.8,0.5,1.3,0.3,1.0,0.3,1.4,1.4,1.1,1.4,0.8,1.2,0.5,1.0,0.4,0.8,0.3,0.7,0.3,1.0,0.6,1.1,0.9,0.7,0.5,0.3,0.3,0.3,0.4)
max <- c(1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,0.6)
df <- data.frame(num,name,x,min,max)

As one can see, the data describes ranges of configurations. What I want to do is something like that: enter image description here

I tried the geom_bar option, but without success at all. Does anyone has an idea, how I can use ggplot for this data and that type of plot?

P.S. The x-axis should be df$x, y-axis should be range(df$min, df$max). The color should depend on df$name.

df$num is just to group the data. Sometimes there is only one df$name for one df$num, sometimes there are two df$name for one df$num.

M--
  • 25,431
  • 8
  • 61
  • 93
Hymir
  • 811
  • 1
  • 10
  • 20
  • 1
    I was taking a look at your questions. [here](https://stackoverflow.com/questions/41882223/non-blocking-pthread-stop-or-why-does-stdatomic-flag-slow-down-my-code) you were supposed to report back on performance or [here](https://stackoverflow.com/questions/42271735/cast-raw-pointer-of-array-to-unique-ptr) the answer clearly resolved your issue and other examples as well. Please read [Why I should accept an answer?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). It is important to contribute to the SO as you are benefiting from it. Cheers. – M-- Jun 21 '17 at 15:49

1 Answers1

0

Your question is a little bit vague as it does not represent an actual desired graph but this would be your answer or a good starting point:

library(ggplot2)
#set the whiskers to min/max as well
#set the middle line to min or max
#set the color to interaction of name and num columns
p <- ggplot(df, aes(x = x, ymin = min, lower=min,
         middle= min, upper=max, ymax = max , fill = interaction(name, num))) +
geom_boxplot(stat="identity") + 
  coord_cartesian(ylim = c(0, 1.5))



library(ggpubr)
ggpar(p, legend.title = "name.num") #just change the legend title

This will give us:

enter image description here

M--
  • 25,431
  • 8
  • 61
  • 93
  • Very nice!!! Thanks alot. Is there a possibility to change the "tick" size? so that the x and y-axis get 0,1 steps? – Hymir Jun 22 '17 at 18:21
  • @Hymir https://stackoverflow.com/questions/11335836/increase-number-of-axis-ticks – M-- Jun 22 '17 at 20:39