R 仅连接某些点的几何图形线
我正在处理的数据可用,或如下所示:R 仅连接某些点的几何图形线,r,if-statement,ggplot2,R,If Statement,Ggplot2,我正在处理的数据可用,或如下所示: Behaviour Repeatability UCI LCI Age stage Activity 0.1890000 0.2470000 0.1600000 PE A Activity 0.5500000 0.7100000 0.3900000 PW B Activity 0.5100000 0.6300000 0.4000000 A D Activity
Behaviour Repeatability UCI LCI Age stage
Activity 0.1890000 0.2470000 0.1600000 PE A
Activity 0.5500000 0.7100000 0.3900000 PW B
Activity 0.5100000 0.6300000 0.4000000 A D
Activity 0.4100000 NA NA A1 D
Activity 0.4229638 0.4561744 0.3854906 CA D
Activity 0.1812492 0.2111999 0.1522250 CY C
Aggression 0.2620000 0.3030000 0.1960000 PE A
Aggression 0.3700000 0.3800000 0.3600000 PW B
Aggression 0.4400000 0.5600000 0.3300000 A D
Aggression 0.3740000 NA NA A1 D
Aggression 0.3212115 0.3471766 0.2801818 CA D
Aggression 0.5106432 0.5635857 0.4634950 CY C
与其他用户(、和)类似,我试图绘制这些数据,以便线只经过特定的点(在我的例子中是黑点)。注:橙色(A
在Age
列中)和红色(A1
在Age
列中)点用于说明我的结果与其他结果的比较
我能得到的最接近的结果是:
获取此绘图的代码为:
pd <- position_dodge(0.3)
ggplot(rep, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) +
geom_point(position = position_dodge(width = 0.3), size = 3) +
geom_line(aes(group=Behaviour), position = position_dodge(width = 0.3))+
scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)")) +
scale_colour_manual(values = c("orange", "red", "black", "black", "black", "black"), name = "Study", breaks=c("A","A1","PE","PW", "CA", "CY")) +
guides(colour=FALSE)+
geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+ theme_bw() +
theme(axis.line = element_line(colour = "black"),
plot.caption = element_text(hjust = 0, vjust = 2.12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
text = element_text(size = 15)) +
labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")
pd正如@IceCreamToucan所指出的,您可以通过过滤来实现这一点,只需包含。相关的年龄系列
(我遇到了一个问题,在生命的最后阶段,点和线之间的对齐不一致。不确定如何解决。)
我将发布最终对我有效的解决方案,以防其他人遇到类似问题
我可以通过首先重命名Age
列中的变量来更改它们的顺序,从而获得所需的绘图。将CA
更改为A1
并将A1
更改为A2
然后,正如@IceCreamToucan和@Jon Spring所建议的:
my_colors <-
tibble(color = c("orange", "black", "red", "black","black", "black"),
Age = c("A","A1","A2", "PE","PW", "CY"))
ggplot(rep,
aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) +
geom_point(position = position_dodge(width = 0.3), size = 3) +
geom_line(aes(group=Behaviour), position = position_dodge(width = 0.3),
data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+
scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)"))) +
guides(colour=FALSE)+ #removes the legend showing colour
geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
plot.caption = element_text(hjust = 0, vjust = 2.12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
text = element_text(size = 15)) +
labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")
my_colors%filter(颜色='black'))+
量表(标签=c(“A”=“50-70天大”,“B”=“70-365天大”,“c”=“一岁(365天大)”,“D”=“成年人(>365天大)”))+
辅助线(颜色=假)+#删除显示颜色的图例
几何误差条(aes(ymin=LCI,ymax=UCI),位置=pd,宽度=0.1,尺寸=0.5)+
主题_bw()+
主题(轴线=元素线(颜色=“黑色”),
plot.caption=element_text(hjust=0,vjust=2.12),
panel.grid.major=元素_blank(),
panel.grid.minor=元素_blank(),
panel.border=元素_blank(),
panel.background=元素_blank(),
文本=元素\文本(大小=15))+
实验室(y=“重复性(r)±95%CrI”,x=“寿命阶段”)
然后给出所需的绘图:
我需要在绘图中保留geom\u errorbar
。正如我的OP所显示的,数据丢失了,我必须绘制一个图,图中每隔一个点仍然显示错误条。当我运行您建议的代码时(但使用geom_errorbar
),绘图仍然无法连接点。@IceCreamToucan我使用的所有东西都有问题。NA是“真实的”。我的意思是,我有两个NAs点仍然需要绘制(那些红色点)。他们没有错误条值,这是“真实”的数据。@IcecreamTou可以很好地捕捉!我在信中加了这个。
my_colors <-
tibble(color = c("orange", "black", "red", "black","black", "black"),
Age = c("A","A1","A2", "PE","PW", "CY"))
ggplot(rep,
aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) +
geom_point(position = position_dodge(width = 0.3), size = 3) +
geom_line(aes(group=Behaviour), position = position_dodge(width = 0.3),
data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+
scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)"))) +
guides(colour=FALSE)+ #removes the legend showing colour
geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
plot.caption = element_text(hjust = 0, vjust = 2.12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
text = element_text(size = 15)) +
labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")