在bar/col图中堆叠多列df [英] Stack multiple columns of df in bar/col plot

查看:37
本文介绍了在bar/col图中堆叠多列df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下df

df <- data.frame(genre = c("Thriller", "Horror", "Action"), europe = c(30, 35, 50), asia = c(20, 15, 25), america = c(50, 50, 25))

     genre europe asia america
1 Thriller     30   20      50
2   Horror     35   15      50
3   Action     50   25      25

我想得到情节

有人有什么建议吗?在此先感谢:D

Does anyone have any suggestions? Thanks in advance :D

推荐答案

使用 pivot_longer 重塑数据:

library(ggplot2)

ggplot(tidyr::pivot_longer(df, -1), aes(genre, value, fill = name)) +
  geom_col(color = "gray20") +
  geom_text(aes(label = paste(value, "%")), fontface = "bold", size = 5,
            position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("#1ca4fc", "#fcad2a", "#595959"), 
                    name = "Continent") +
  theme(text = element_text(face = "bold", size = 18))

这篇关于在bar/col图中堆叠多列df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆