我想将错误条的颜色更改为不同颜色而不更改图表上各点的位置。
以下是我的一些数据:
df <- structure(
list(
yrmonth = structure(
c(
1483228800,
1483228800,
1483228800,
1485907200,
1485907200,
1485907200,
1488326400,
1488326400,
1488326400,
1491004800,
1491004800,
1491004800
),
class = c("POSIXct", "POSIXt"),
tzone = "UTC"
),
index = structure(
c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L),
.Label = c("N-S", "N-S", "E-W", "E-W", "OS"),
class = "factor"
),
N = c(2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1),
GDDLettuce = c(129, 141, 27, 150.5, 209, 87, 247.5,
243, 188, 223, 226.5, 170),
sd = c(
1.4142135623731,
4.24264068711928,
NA,
4.94974746830583,
65.0538238691624,
NA,
12.0208152801713,
8.48528137423857,
NA,
5.65685424949238,
0.707106781186548,
NA
),
se = c(1, 3, NA, 3.5, 46, NA, 8.5, 6, NA, 4, 0.5, NA),
ci = c(
12.7062047361747,
38.1186142085241,
NA,
44.4717165766114,
584.485417864036,
NA,
108.002740257485,
76.2372284170481,
NA,
50.8248189446988,
6.35310236808735,
NA
)
),
.Names = c("yrmonth", "index", "N", "data", "sd", "se", "ci"),
row.names = 31:42,
class = "data.frame"
)
我的图表设置完全符合我想要的方式,并在正确的位置显示错误条:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), width = 1000000, size = .5,
position = position_dodge(width = -300000))
当我添加一种颜色时,条形的颜色会发生变化,但会删除我放入的所有宽度和闪避参数:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), colour = "black", width = 1000000, size = .5,
position = position_dodge(width = -300000))
我希望E-W误差条为深绿色,而N-S误差条为深红色。我希望线条本身保持相同的颜色。最重要的是,我希望误差线保持在相同的位置。出于某种原因,每次我改变颜色时,我的错误条和线的位置都会改变。
答案 0 :(得分:1)
尝试在您的第一个地图代码中再添加一个图层scale_color_manual
,如下所示:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), width = 1000000, size = .5,
position = position_dodge(width = -300000))+
scale_color_manual(values = c('darkblue', 'darkgreen', 'darkred'))
答案 1 :(得分:1)
快速简便的解决方案是
require(dplyr)
ggplot(df %>% mutate(errorColors = ifelse(index=="N-S","darkred",ifelse(index=="E-W","darkgreen",NA))), aes(x=yrmonth,y=data)) +
geom_line(aes(group=index,color=index),size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se,color=errorColors), width = 1000000, size = .5,
position = position_dodge(width = -300000))+scale_color_manual(breaks=c("E-W","N-S","OS"),values=c("darkgreen","darkred","green","red","blue"))