簡體   English   中英

如何組合柱形圖

[英]How to combine columns bar chart

我需要使用我現有的數據來繪制這個圖表。

在此處輸入圖像描述

這就是我的數據的樣子。

在此處輸入圖像描述

這是錯誤:

在此處輸入圖像描述

這是我嘗試過的:

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") 

使用您的數據,我能夠生成以下內容。

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()
  )

代表 package (v0.3.0) 於 2021 年 2 月 23 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM