2017-04-04 57 views
7

我想图线从参考点发散“a”到其它点如“B”,“C”,“d”等,ggplot发散线路用误差棒

数据:

df <- structure(list(value = c(1.40438297796257, 1.44036790976986, 
1.37704383251482, 1.45355096018748, 1.40847559339844, 1.38860635968641, 
1.43714387291229), group = c("a", "b", "c", "d", "e", "f", "g" 
), low = c(1.38956448514689, 1.40198829989962, 1.33523395978584, 
1.42008027933896, 1.37516232159193, 1.34823916425279, 1.397985577859 
), up = c(1.41920147077825, 1.4787475196401, 1.4188537052438, 
1.487021641036, 1.44178886520494, 1.42897355512002, 1.47630216796558 
), sem = c(0.00757411399256711, 0.0120426947992103, 0.0137959906464809, 
0.00953361452671253, 0.00945315870421568, 0.0130586010600045, 
0.0124407008862053)), .Names = c("value", "group", "low", "up", 
"sem"), row.names = c(NA, -7L), class = "data.frame") 

代码:

library('ggplot2') 
ggplot(df, aes(x = group, y = value, group = 1)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
       aes(ymin = value - sem, ymax = value + sem), 
       colour="red") + 
    geom_point(shape = 21, size = 4, fill="white") 

当前打印:

enter image description here

预计剧情:

enter image description here

+1

我相信'ggplot'群体之间在这种情况下 “点连接”。这就是为什么你需要重复参考值,如下面OganM的答案。这是为了在'a'和'b'之间划一条线,你需要观察'a = group1'和'b = group1'。类似地,在'a'和'c'之间绘制你需要一个观察,其中'a = group2'和'c = group2'等。 –

回答

8

不知道你为什么这样做group = 1但你需要的group VAR的线分开。在这里,我创建了与第一个数据点相同的虚拟数据点,与每个数据点位于同一组。请注意,如果您打算使用透明度,这将导致问题,并需要进一步的摆弄。

df = rbind(df[rep(1,5),],df) 

df$lineGroup = c(1:6,1:6) 

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red") + 
    geom_point(shape = 21, size = 4, fill="white") 

enter image description here

透明度问题

如果你

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black",alpha=.3) + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red",alpha =.3) + 
    geom_point(shape = 21, size = 4, fill="white") 

你会看到第一点是暗由于多个数据点有

存在

enter image description here

要摆脱这种情况,您需要通过aes控制透明度,并添加一个控制可见性的列。

df$alpha = c('visible', rep('hidden',5), rep('visible',6)) 

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up,alpha= alpha), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem,alpha=alpha), 
        colour="red") + 
    scale_alpha_manual(name='',values = c('visible' = 0.3,'hidden' = 0)) + 

    geom_point(aes(), shape = 21, size = 4, fill="white") 

enter image description here

+0

我使用'group = 1'来避免这个错误'geom_path:每个组包含只有一个观察。你是否需要 调整群体审美?' – Sathish

+0

在解释中编辑 – OganM

2

使用相同的数据和方法如上OganM的回答,您可以通过在geom_point使用去欺骗数据集中解决透明度问题。这应该工作:

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red") + 
    geom_point(data = df[!duplicated(subset(df,select=-lineGroup)),], 
       shape = 21, size = 4, fill="white") 

enter image description here

数据:

df<-structure(list(value = c(1.40438297796257, 1.40438297796257, 
1.40438297796257, 1.40438297796257, 1.40438297796257, 1.40438297796257, 
1.44036790976986, 1.37704383251482, 1.45355096018748, 1.40847559339844, 
1.38860635968641, 1.43714387291229), group = c("a", "a", "a", 
"a", "a", "a", "b", "c", "d", "e", "f", "g"), low = c(1.38956448514689, 
1.38956448514689, 1.38956448514689, 1.38956448514689, 1.38956448514689, 
1.38956448514689, 1.40198829989962, 1.33523395978584, 1.42008027933896, 
1.37516232159193, 1.34823916425279, 1.397985577859), up = c(1.41920147077825, 
1.41920147077825, 1.41920147077825, 1.41920147077825, 1.41920147077825, 
1.41920147077825, 1.4787475196401, 1.4188537052438, 1.487021641036, 
1.44178886520494, 1.42897355512002, 1.47630216796558), sem = c(0.00757411399256711, 
0.00757411399256711, 0.00757411399256711, 0.00757411399256711, 
0.00757411399256711, 0.00757411399256711, 0.0120426947992103, 
0.0137959906464809, 0.00953361452671253, 0.00945315870421568, 
0.0130586010600045, 0.0124407008862053), lineGroup = c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L)), .Names = c("value", 
"group", "low", "up", "sem", "lineGroup"), row.names = c("1", 
"1.1", "1.2", "1.3", "1.4", "11", "2", "3", "4", "5", "6", "7" 
), class = "data.frame")