手动更改线型顺序并抵消ggplot中的误差线 [英] Manually changing linetype order and offsetting error bars in ggplot

查看:101
本文介绍了手动更改线型顺序并抵消ggplot中的误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这些数据

Data <- structure(list(value = c(180, 528, 180, 147, 468, 151, 194, 568, 
210), SE = c(21.7869586486209, 21.0831764730322, 21.2726560659361, 
21.7869586486209, 21.0831764730322, 21.2726560659361, 21.7869586486209, 
21.0831764730322, 21.2726560659361), PredictionType = c("InSitu", 
"ExSitu", "ExSitu", "ExSitu", "InSitu", "ExSitu", "ExSitu", "ExSitu", 
"InSitu"), Area = c("AAA", "BBB", "CCC", "AAA", "BBB", "CCC", 
"AAA", "BBB", "CCC")), .Names = c("value", "SE", "PredictionType", 
"Area"), class = "data.frame", row.names = c(NA, -9L))

和下面的代码,我可以在下面生成图.

ggplot(Data)+
  geom_point(aes(x=Area, y=value, color=Area),size=3, shape=1)+
  geom_errorbar(aes(x=Area, ymin=value-SE, ymax=value+SE, color=Area, linetype = PredictionType),cex=0.75)

线型可以正确区分ExSitu和InSitu预测,但是我希望线型可以颠倒.明确地说,我希望虚线是ExSitu,实线是InSitu.

此外,是否可以使误差线偏移,以使误差线不直接位于彼此之上?理想情况下,我希望实心InSitu估算值以区域(AAA,BBB,CCC)为中心,而两个点缀的ExSitu估算值则稍微居中于中心.

先谢谢了.

解决方案

您可以使用position参数来偏移(闪避)点和误差线(请参见例如dodge -ing起作用.在这里,我使用ave创建虚拟变量.我借用"未使用的aes主题fill进行闪避,然后删除填充图例.在scale_linetype_manual中更改linetype的使用顺序.

Data$id <- with(Data, ave(Area, Area, FUN = seq_along))
dodge <- position_dodge(width = 0.4)

ggplot(Data, aes(x = Area, y = value, fill = id, colour = Area, linetype = PredictionType)) +
  geom_point(size = 3, shape = 1, position = dodge) +
  geom_errorbar(aes(ymin = value - SE, ymax = value + SE),
                cex = 0.75, position = dodge) +
  scale_linetype_manual(values = c("dotted", "solid")) +
  scale_fill_discrete(guide = FALSE) 

更新以下评论:

为了将"InSitu"点放置在中间,一种可能性是将"InSitu"数据的id变量设置为2,将"ExSitu"数据的id变量分别设置为1和3:

Data <- Data[order(Data$Area, Data$PredictionType), ]
Data$id <- as.character(c(1, 3, 2))

ggplot(Data, aes(x = Area, y = value, fill = id, colour = Area, linetype = PredictionType)) +
  geom_point(size = 3, shape = 1, position = dodge) +
  geom_errorbar(aes(ymin = value - SE, ymax = value + SE),
                size = 1, width = 0.5, position = dodge) +
  scale_linetype_manual(values = c("dotted", "solid")) +
  scale_fill_discrete(guide = FALSE)

Using these data

Data <- structure(list(value = c(180, 528, 180, 147, 468, 151, 194, 568, 
210), SE = c(21.7869586486209, 21.0831764730322, 21.2726560659361, 
21.7869586486209, 21.0831764730322, 21.2726560659361, 21.7869586486209, 
21.0831764730322, 21.2726560659361), PredictionType = c("InSitu", 
"ExSitu", "ExSitu", "ExSitu", "InSitu", "ExSitu", "ExSitu", "ExSitu", 
"InSitu"), Area = c("AAA", "BBB", "CCC", "AAA", "BBB", "CCC", 
"AAA", "BBB", "CCC")), .Names = c("value", "SE", "PredictionType", 
"Area"), class = "data.frame", row.names = c(NA, -9L))

and the following code I can produce the plot below.

ggplot(Data)+
  geom_point(aes(x=Area, y=value, color=Area),size=3, shape=1)+
  geom_errorbar(aes(x=Area, ymin=value-SE, ymax=value+SE, color=Area, linetype = PredictionType),cex=0.75)

The linetype is correctly distinguishing between ExSitu and InSitu predictions, but I want the line type to be reversed. Equivocally, i want the dotted line to be ExSitu and the solid line to be InSitu.

Also, is it possible to offset the error bars so that they are not directly on top of each other? Ideally I would want the solid InSitu estimate centered over the area (AAA, BBB, CCC) and the two dotted ExSitu estimates slightly right and left of center.

Thanks in advance.

解决方案

You can use position argument to offset (dodge) points and error bars (see e.g. here). However, you have more than one point/error bar with the same 'PredictionType' within each 'Area', and need to create a new variable with a unique value for each point within Area to make the dodge-ing work. Here I use ave to create the dummy variable. I 'borrow' an unused aesthetics, fill, for the dodging, and then remove the fill legend. The order in which the linetypes are used is changed in scale_linetype_manual.

Data$id <- with(Data, ave(Area, Area, FUN = seq_along))
dodge <- position_dodge(width = 0.4)

ggplot(Data, aes(x = Area, y = value, fill = id, colour = Area, linetype = PredictionType)) +
  geom_point(size = 3, shape = 1, position = dodge) +
  geom_errorbar(aes(ymin = value - SE, ymax = value + SE),
                cex = 0.75, position = dodge) +
  scale_linetype_manual(values = c("dotted", "solid")) +
  scale_fill_discrete(guide = FALSE) 

Update following comment:

In order to place the 'InSitu' points in the middle, one possibility is to set the id variable to 2 for 'InSitu' data, and to 1 and 3 respectively for 'ExSitu' data:

Data <- Data[order(Data$Area, Data$PredictionType), ]
Data$id <- as.character(c(1, 3, 2))

ggplot(Data, aes(x = Area, y = value, fill = id, colour = Area, linetype = PredictionType)) +
  geom_point(size = 3, shape = 1, position = dodge) +
  geom_errorbar(aes(ymin = value - SE, ymax = value + SE),
                size = 1, width = 0.5, position = dodge) +
  scale_linetype_manual(values = c("dotted", "solid")) +
  scale_fill_discrete(guide = FALSE)

这篇关于手动更改线型顺序并抵消ggplot中的误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆