【问题标题】:Plotting two bar charts next to each other in R [duplicate]在R中绘制两个相邻的条形图[重复]
【发布时间】:2021-01-11 18:33:06
【问题描述】:

我正在尝试使用df = melt 绘制两个相邻的条形图,但我不知道如何正确使用它。

这是我创建的两个条形图:

unemployment_rate_averages<-
ggplot(data = urate_data,
       aes(x=regions, y=averages), fill = grp) +
  geom_bar(stat="identity")

【问题讨论】:

  • 你是什么意思'彼此相邻'。就像你并排展示的两个地块一样?
  • 没错!并排
  • 这能回答你的问题吗? ggplot side by side geom_bar()
  • 如果是两个独立的 grobs,patchwork 不是很合适吗?也就是说,library(patchwork); combined_grob &lt;- unemployment_rate_averages + unemployment_rate_changes,您可以像对待任何其他 grob(我认为)一样对待 combined_grob,包括在控制台上查看 combined_grob
  • 对不起,我删除了我的答案,因为我认为它是重复的,但如果不是,urate_data %&gt;% tidyr::pivot_longer(-regions) %&gt;% ggplot(aes(x=regions, y=value)) + geom_bar(stat='identity') + facet_wrap(~name) 会做你需要的。

标签: r ggplot2 bar-chart


【解决方案1】:
urate_data %>% 
    tidyr::pivot_longer(-regions) %>% 
    ggplot(aes(x=regions, y=value)) + 
    geom_bar(stat='identity') + 
    facet_wrap(~name)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2023-02-04
    相关资源
    最近更新 更多