ggplot:闪避时不会出现错误栏

时间:2015-09-24 00:42:18

标签: r graphics

我有一个可怕的时间得到错误条形图正确。涉及重叠功能(躲避)的事情会造成麻烦吗?

Data:
 mean mean_b    se  se.1   seb seb.1 ID
1 0.52   0.20 0.137 0.137 0.015 0.015  1
2 0.17   0.20 0.062 0.062 0.016 0.016  2
3 0.46   0.60 0.078 0.078 0.006 0.006  3
4 0.34   0.11 0.134 0.134 0.005 0.005  4
5 0.22   0.10 0.066 0.066 0.004 0.004  5
6 0.62   0.14 0.083 0.083 0.003 0.003  6
7 0.11   0.29 0.133 0.133 0.065 0.065  7
8 0.51   0.44 0.113 0.113 0.026 0.026  8
9 0.41   0.50 0.082 0.082 0.009 0.009  9

# grab data for data A
    df_m <- data[ , c(7, 1, 3, 4)]
    df_m$comp <- "Initial Occupancy"
    names(df_m) <- c("ID", "avg", "lower", "upper", "comp")


# grab data for data B
     df_f <- data[ , c(7, 2, 5, 6)]
    df_f$comp <- "Equilibrium Occupancy"
    names(df_f) <- c("ID", "avg", "lower", "upper", "comp")

# bind the data together
    df <- rbind(df_m, df_f)

# plot
    ggplot(data = df, aes(x = ID, y = avg, ymin = lower, ymax = upper, colour = comp)) +
      geom_point(position = position_dodge(width = 0.4)) +
     geom_errorbar(position = position_dodge(width = 0.4), width = .3) +
     coord_flip() +
     scale_colour_manual(values = c("blue", "red")) +
       theme_bw() +
    theme(panel.grid.major.y = element_line(colour = "grey", linetype =   "dashed"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) 

1 个答案:

答案 0 :(得分:0)

data=read.csv()
# grab data for males
df_m <- data[ , c(12, 1, 3)]
df_m$comp <- "Initial Occupancy"
names(df_m) <- c("ID", "avg", "se", "comp")
df_m
# grab data for females
df_f <- data[ , c(12, 2, 5)]
df_f$comp <- "Equilibrium Occupancy"
names(df_f) <- c("ID", "avg", "se", "comp")
df_f
# bind the data together
df <- rbind(df_m, df_f)
# plot
ggplot(data = df, aes(x = ID, y = avg, ymin = avg-se, ymax = avg+se, colour = comp)) +
geom_point(position = position_dodge(width = 0.4),pch=21) +
geom_errorbar( position = position_dodge(width = 0.4), width = .3) +
coord_flip() +
scale_colour_manual(values = c("blue", "red")) +
#theme_classic()
theme_bw() +
theme(panel.grid.major.y = element_line(colour = "grey", linetype = "dashed"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())

谢谢你jlhoward!