我想绘制从引用点“a”到其他点(例如“b”、“c”、“d”等)的线条,
数据:
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")
当前情节:
预期情节:
最佳答案
不知道你为什么做 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")
透明度问题
如果你这样做
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")
由于存在多个数据点,您会看到第一个点更暗
要消除这种情况,您还需要通过添加一个控制可见性的列来通过
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")
关于r - ggplot 发散线与误差线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43218907/