我正在尝试更改包含两条曲线和两条垂直线的绘图中的线条颜色,但它不成功。你介意我告诉我如何更改下面的脚本,为4行中的每一行分配一个特定的颜色吗?非常感谢你的帮助!
data <- data.frame(day_of_year=rep(1:5, 4), variable=c('variable1', 'variable1', 'variable1', 'variable1', 'variable1', 'variable2', 'variable2', 'variable2', 'variable2', 'variable2', 'variable3', 'variable3', 'variable3', 'variable3', 'variable3', 'variable4', 'variable4', 'variable4', 'variable4', 'variable4'), value=c(12, 21, 35, 42, 56, 1, 4, 3, 4, 5, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4))
p1 <- ggplot(data=data)
p1 <- p1 + geom_line(data=data[data$variable=='variable1',], aes(x=day_of_year, y=value, colour='variable1'), size=0.5)
p1 <- p1 + geom_line(data=data[data$variable=='variable2',], aes(x=day_of_year, y=value*10, colour='variable2'), size=0.5)
p1 <- p1 + geom_vline(data=data[data$variable=='variable3',], aes(xintercept=value, colour='variable3'), linetype='dashed', size=0.5)
p1 <- p1 + geom_vline(data=data[data$variable=='variable4',], aes(xintercept=value, colour='variable4'), linetype='dashed', size=0.5)
p1 <- p1 + scale_colour_discrete(breaks=c("variable1","variable2","variable3", 'variable4'))
p1 <- p1 + labs(list(colour = "", title = "(A)", x = "Day of year", y =""))
p1 <- p1 + theme_bw()
p1 <- p1 + theme(title=element_text(size=7, face='bold', vjust=0.8, hjust = 0.5), axis.text.x=element_text(size=7), axis.text.y =element_text(size=8),legend.text=element_text(size=8),legend.title=element_text(size=8), axis.title=element_text(size=8, face='bold'))
p1 <- p1 + geom_text(x=1.5, y=50, label='fallow', size=3)
p1 <- p1 + geom_text(x=3, y=50, label='crop', size=3)
p1 <- p1 + geom_text(x=4.5, y=50, label='fallow',size=3)
p1
我尝试添加以下脚本,但没有成功:
# p1 <- p1 + scale_colour_manual(values=c('red','black','dark grey','blue'))
# p1 <- p1 + scale_colour_discrete(value=c("blue", "red", "grey", 'black'))
# p1 <- p1 + scale_color_manual(value=c("#CC6666", "#9999CC", "#CC6666", "#9999CC"), breaks=c("variable1","variable2","variable3", 'variable4'))
答案 0 :(得分:2)
就像@MrFlick一样,您的代码使用您提供的示例数据为我工作(除了value
与values
错误之外,三次改变颜色的尝试中有两次尝试。
通常情况下,您只需要拨打geom_line
一次,然后colour=variable
就会为variable
的每个级别创建单独的行。但是在这里看起来你想要对variable
的每个级别做一些不同的事情。在任何情况下,这里都是对代码的修改,以使其更简洁,并且包含scale_colour_manual
。
p1 <- ggplot() +
geom_line(data=d[d$variable=='variable1',],
aes(x=day_of_year, y=value, colour=variable), size=0.5) +
geom_line(data=d[d$variable=='variable2',],
aes(x=day_of_year, y=value*10, colour=variable), size=0.5) +
geom_vline(data=d[d$variable %in% c('variable3','variable4'),],
aes(xintercept=value, colour=variable), linetype='dashed',
size=0.5, show.legend=FALSE) +
scale_colour_manual(values=c('red','black','dark grey','blue')) +
labs(list(colour = "", title = "(A)", x = "Day of year", y ="")) +
theme_bw(base_size=8) +
theme(title=element_text(size=7, face='bold', vjust=0.8, hjust = 0.5),
axis.text.x=element_text(size=7),
axis.title=element_text(face='bold')) +
geom_text(data=data.frame(x=c(1.5,3,4.5), y=50),
aes(x,y), label=c('Fallow','Crop','Fallow'), size=3)
答案 1 :(得分:0)
尝试此格式化..您可以更改任何参数以查看绘图如何更改
geom_line(mapping=aes(y = y, color = y), size = 1.11) + theme(panel.grid.major = element_line(colour = "blue", size = 1.25)) +
theme(axis.ticks = element_line(size = 5, colour = "black")) +
scale_fill_discrete() + scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
labs(colour = legend.title) + theme(plot.title = element_text(size = rel(1.76))) +
guides(colour = guide_legend(override.aes = list(size=3))) +
scale_y_continuous(breaks=number_ticks(10)) +
theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"),
axis.text.x = element_text(face="bold", color="black", size=24, angle=25),
axis.text.y = element_text(face="bold", color="black", size=24, angle=0))