0

I need to draw this chart by using my existing data.

enter image description here

This is what my data looks like.

enter image description here

This is the error:

enter image description here

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") 
Phil
  • 5,491
  • 3
  • 26
  • 61
Rachael
  • 27
  • 1
  • 1
    In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Feb 24 '21 at 01:26

1 Answers1

0

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)

iamericfletcher
  • 2,529
  • 6
  • 17