当我们有许多绘图GGPLOT2R时,将背景颜色更改为三个区域
我有以下代码生成下面的图:当我们有许多绘图GGPLOT2R时,将背景颜色更改为三个区域,r,ggplot2,R,Ggplot2,我有以下代码生成下面的图: ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) + coord_flip() + scale_fill_gradient(low = "red", high = "green") + geom_bar(stat = "identity") + geom_point(aes(y=Fa
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
我从stackoverflow上的其他线程知道,我需要从空ggplot()
开始,然后添加annotate
。我试图这样做,并为每个绘图说明数据
和aes
,但我收到了很多错误消息
请告诉我如何修改上面的代码以将三个背景区域发送到后面。Ggplot将层添加到彼此的顶部,因此如果我正确理解您的问题,您可以通过在+geom_point()和+geom_bar()之前放置+annotate()来解决此问题,如下所示:
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
当我试图将
注释
放在其他绘图之前时,我会收到一条错误消息,上面写着:“错误:离散值提供给连续刻度。”。但是,我通过使用scale\u x\u discrete()
向ggplot2指定我对x轴使用离散比例,成功地解决了这个问题
ggplot(data = factor_output) +
scale_x_discrete() +
coord_flip() +
annotate(geom = "rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.4, fill = c("LightGreen", "LightGreen", "Orange")) +
scale_fill_gradient(low = "red", high = "green") +
scale_y_continuous(breaks=c(-0.75, -0.5, -0.3, 0, 0.3, 0.5, 0.75)) +
geom_bar(aes(y = F.Difference, x = reorder(Action.Title, F.Difference), fill=Efficacy.Median), stat = "identity") +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor1, colour="DarkRed"), size = 3) +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor2, colour="DarkBlue"), size = 3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
ggtitle("XXX") +
xlab("XXX") +
ylab("XXX") +
theme_bw() +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title = element_text(size = 15),
legend.text = element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill = "transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
您好,我在发布我的帖子之前尝试过这个,但我收到了以下消息:“错误:离散值提供给连续缩放。”