在ggplot中合并两个图?

时间:2019-04-16 15:40:17

标签: r ggplot2 plot

我需要创建一个ggplot,该ggplot是覆盖有折线图的柱形图。线形图显示平均值,而柱形图显示平均值与基准值的关系。我已经设法在ggplot中创建了两个单独的图,但是在合并它们时遇到了麻烦。

我的线条图如下:

enter image description here

并使用以下代码创建:

benchMarkLine <- ggplot(UEQScores, aes(x=Scale, y=Score, group=1)) +
  geom_line(size = 1.4, colour = "black") +
  geom_point(size = 2.4, colour = "black") +
  scale_y_continuous(name = "Score", breaks = seq(0, 2.5, 0.25), limits = c(0, 2.5)) +
  scale_x_discrete(name = "Scale") +
  ggtitle("Mean Scores") +
  theme_bw() + # Set black and white theme +
  theme(plot.title = element_text(hjust = 0.5, size=10), # Centre plot title
        panel.grid.major = element_blank(), # Turn off major gridlines
        panel.grid.minor = element_blank(), # Turn off minor gridlines
        axis.title.x = element_text(size=10),
        axis.text.x = element_text(angle=30, vjust=0.6),
        axis.title.y = element_text(size=10))
benchMarkLine

我的专栏图如下:

enter image description here

并使用以下代码创建:

benchmarkColPlot <- ggplot(benchmark_long, aes(x=factor(Scale, scaleLevels), y=value, fill=factor(cat, bmLevels))) + 
  geom_col(position="fill") +
  scale_fill_manual(values = bmColours) +
  scale_y_continuous(name = "Score", breaks = seq(-1.0, 1.0, 0.25), limits = c(-1, 1)) +
  scale_x_discrete(name = "Scale") +
  ggtitle("Benchmark Scores") +
  theme_bw() + # Set black and white theme +
  theme(plot.title = element_text(hjust = 0.5, size=10), # Centre plot title
        panel.grid.major = element_blank(), # Turn off major gridlines
        panel.grid.minor = element_blank(), # Turn off minor gridlines
        axis.title.x = element_text(size=10),
        axis.text.x = element_text(angle=30, vjust=0.6),
        axis.title.y = element_text(size=10),
        legend.title = element_blank())
benchmarkColPlot

如何将两者结合?我尝试将geom_line(UEQScores, aes(x=Scale, y=Score, group=1)) +上方的geom_col(position="fill") +插入列绘图代码中,但出现以下错误:

Error: `mapping` must be created by `aes()`

如何合并这两个图?


好的,我已经放弃了-我只是在Excel中创建了图表,因为对于我在这里所做的操作来说,它似乎要容易一些。

0 个答案:

没有答案