2

I'd like to make a simple graph: mean in the middle and min and max as whiskers. No box. What is the easiest way to do it? Thanks.

structure(list(Country = structure(c(1L, 9L, 6L, 5L, 3L, 8L, 
7L), .Label = c("BU", "CZ", "ES", "HU", "LT", "LV", "PL", "SL", 
"UK"), class = "factor"), Mean = c(0.68, 0.56, 0.44, 0.31, 0.27, 
0.8, 0.13), Min = c(0.44, 0.34, -0.35, -0.05, -0.16, 0.76, -0.44
), Max = c(0.85, 0.83, 0.83, 0.84, 0.55, 0.85, 0.84)), .Names = c("Country", 
"Mean", "Min", "Max"), row.names = c(1L, 2L, 3L, 4L, 5L, 8L, 
9L), class = "data.frame")
Lybica
  • 79
  • 7
  • 1
    take a look here...http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars – astrosyam Jan 18 '16 at 01:47
  • `boxplot(rnorm(100), pars = list(boxcol = 'transparent'))` or `boxplot(rnorm(100), pars = list(boxlty = 0))` see `?bxp` for more options not covered in `?boxplot` – rawr Jan 18 '16 at 01:49
  • Thank you @astrosyam so much. Didn't occur to me they are called error bars. Got it done! – Lybica Jan 18 '16 at 02:43
  • Dear @rawr, this is a nice way but whiskers don't go out of the mean value, leaving the gaps. Thank you anyway. – Lybica Jan 18 '16 at 02:45

3 Answers3

1

That's how i did it, thanks to @astrosyam.

corr1 = corr[c(1:5, 8:9),1:4] # to remove NAs
# to order the cournties the way I need
Country1 = factor(corr1$Country, levels(corr1$Country)[c(1,9,6,5,3,8,7)])
x = c(1,2,3,4,5,6,7,8,9)

plot(Country1, corr1$Mean, pch=19, ylim=range(c(corr1$Mean-corr1$Min, corr1$Mean+corr1$Min)))
# hack: we draw arrows but with very special "arrowheads"
arrows(x, corr1$Mean-corr1$Min, x, corr1$Mean+corr1$Min, length=0.05, angle=90, code=3)
Lybica
  • 79
  • 7
1

Here's a hackish way of getting it done using bxp:

bxp(
  list(
    stats=rbind(df$Min,df$Max,df$Mean,df$Min,df$Max),
    n=seq_len(nrow(df)),
    names=df$Country
  ),
  lty=1,
  boxlty=0
)

enter image description here

thelatemail
  • 91,185
  • 12
  • 128
  • 188
0

One way to do it with ggplot is the recast the data in long format using tidyr packages gather function and use as follows:

library(tidyr)
library(ggplot2)
df <- df %>% gather(Type, Value, -Country)
ggplot(df, aes(x = Country, y = Value, col = Country)) +
  geom_line() +
  geom_point(size = 2) +
  theme(legend.position = 'none')

enter image description here

Gopala
  • 10,363
  • 7
  • 45
  • 77
  • Thank you! For whatever reason, it didn't work with my data but I appreciate your help anyway. – Lybica Jan 18 '16 at 02:46
  • I reproduced it exactly with your data and posted the answer only after it worked on your data. So, not sure what problem you are having. Can you explain what problems/errors you got? – Gopala Jan 18 '16 at 02:47
  • Thank you. Here is what I got: > df <- df %>% gather(Type, Value, -Country) Error: All select() inputs must resolve to integer column positions. The following do not: * -Country In addition: Warning message: In Ops.factor(Country) : ‘-’ not meaningful for factors – Lybica Jan 18 '16 at 02:55
  • Looks like it doesn't like that countries are not integer. – Lybica Jan 18 '16 at 02:56
  • @Lybica It works with the example you provided. –  Jan 18 '16 at 02:57
  • It is possible the real data has some issues. – Gopala Jan 18 '16 at 02:59
  • The same: Error: All select() inputs must resolve to integer column positions. The following do not: * -Country In addition: Warning message: In Ops.factor(Country) : ‘-’ not meaningful for factors – Lybica Jan 18 '16 at 03:01
  • Don't know what to say... May be I am missing something obvious for you guys. Thanks for the help anyway. – Lybica Jan 18 '16 at 03:04
  • @user3949008, Could you paste a screenshot of your console so I make sure I do exactly the same? Thank you! – Lybica Jan 18 '16 at 03:46