尝试在ggplot2中复制列线图

时间:2018-09-25 15:02:02

标签: r ggplot2

我正在尝试复制此情节:

My desired plot

我可以设置颜色

 scale_color_gradient2()

但是我没有找到如何很好地设置其格式以仅显示x轴和标题居中的方法。我已经使用以下示例进行了一些测试:

library("reshape")
library("scales")
min <- 10
max <- 200
a <- seq(from = min, to = max, by = 5)

b <- 1:length(a)
b <- rep("var_a", length(a))
df <- as.data.frame(cbind(a,b))
df <- df[order(df$a),]

ggplot(df,aes(x = b, y = b,fill = as.numeric(a))) + 
  scale_fill_gradient2() +
  geom_bar(position = "fill",stat = "identity") +
  coord_flip() +
  scale_color_gradient2(midpoint=mid, low="blue", mid="white",
                        high="red", space ="Lab" )+
  ggtitle("test title")

结果是:

enter image description here

1 个答案:

答案 0 :(得分:2)

在这里,我试图模仿您的情节示例。

ggplot(df,aes(x = b, y = 1:nrow(df), fill = as.numeric(a))) + 
  scale_fill_gradient2() +
  geom_bar(position = "fill", stat = "identity", width = 1) +
  coord_flip() +
  scale_color_gradient2(midpoint=mid, low="blue", mid="white",high="red", space ="Lab" )+
  ggtitle("test title") +
  theme_minimal() +
  scale_x_discrete(name = "") +
  scale_y_continuous(name = "") +
  theme(panel.grid = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  guides(fill = FALSE)

enter image description here