我想将参考点“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")
当前情节:
预期情节:
答案 0 :(得分: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")
透明度问题
如果你这样做
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")
答案 1 :(得分: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")
数据:强>
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")