简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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