R 在ggplot中手动更改线型顺序和偏移错误条
使用这些数据R 在ggplot中手动更改线型顺序和偏移错误条,r,ggplot2,R,Ggplot2,使用这些数据 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.2726
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))
线型正确区分了现场预测和现场预测,但我希望线型颠倒。模棱两可地说,我希望虚线是原位的,实线是原位的
此外,是否可以偏移误差条,使它们不直接位于彼此的顶部?理想情况下,我希望现场评估以该区域为中心(AAA、BBB、CCC),两个点式现场评估稍微位于中心的右侧和左侧
提前谢谢 ggplot将PredictionType强制指定给一个因子,然后将因子的每个级别按照级别出现的顺序指定给线型。默认情况下,将ExSitu放在第一位,因此您可以通过几种方式修复它 首先,您可以自己将PredictionType转换为因子,并直接指定所需的级别:
Data$PredictionType = factor(Data$PredictionType,levels=c('InSitu','ExSitu'))
或者,您可以在将其交给ggplot时调用convert right(尽管您可能需要更改图例标题):
您可以使用
position
参数来偏移(闪避)点和错误条(参见示例)。但是,在每个“区域”内有多个具有相同“PredictionType”的点/错误条,并且需要为区域内的每个点创建一个具有唯一值的新变量,以使减淡
-ing工作。这里我使用ave
创建虚拟变量。我“借用”了一个未使用的aes
thetics,fill
,用于躲避,然后删除fill图例。使用线型的顺序在scale\u linetype\u手册
中更改
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)
Data$id谢谢@Henrick。是否可以控制道奇,使实线始终位于BBB区域的中心?
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 = factor(PredictionType,levels=c('InSitu','ExSitu'))),cex=0.75)
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)
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)