简体   繁体   English

如何组合柱形图

[英]How to combine columns bar chart

I need to draw this chart by using my existing data.我需要使用我现有的数据来绘制这个图表。

在此处输入图像描述

This is what my data looks like.这就是我的数据的样子。

在此处输入图像描述

This is the error:这是错误:

在此处输入图像描述

This is what I tried:这是我尝试过的:

percent_incarcerated <- c(0, 5)
  top_10_black_incarceration_plot <- ggplot(top_10_black_incarceration_states_df)+
  geom_col(aes(x = state, y = percent_incarcerated, fill = c(Black, Total)),
       position = "dodge", stat="identity") + 
  geom_col(position="dodge", stat="identity") 

Using your data, I was able to generate the following.使用您的数据,我能够生成以下内容。

df <- data.frame(
  State = c('LA', 'DC', 'MS', 'GA', 'AL', 'KY', 'PA', 'SC'),
  Black = c(0.6, 0.4, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2),
  Total = c(1.0, 0.4, 0.7, 0.6, 0.5, 0.8, 0.4, 0.4)
)


library(ggplot2)
library(dplyr)
library(tidyr)

df_long <-df %>%
  pivot_longer(cols = c(Black, Total), names_to = 'Type', values_to = 'Percent_Incarcerated')

df_long %>%
  ggplot(aes(x = Percent_Incarcerated, y = State, fill = Type)) +
  geom_bar(position = 'dodge', stat = 'identity') +
  scale_fill_manual(values = c('black', 'red')) +
  scale_x_continuous(labels = function(x) paste0(x, '%')) +
  labs(
    title = 'States with Highest Rate of Black Incarceration',
    x = 'Percent Incarcerated'
  ) +
  theme(
    legend.title = element_blank()
  )

Created on 2021-02-23 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2021 年 2 月 23 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM