在ggplot中使用..prop ..时如何绘制彩条图? [英] How to color bar plots when using ..prop.. in ggplot?

查看:49
本文介绍了在ggplot中使用..prop ..时如何绘制彩条图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ggplot上绘制显示比例的条形图.我使用以下方式调用剧情:

  v2<-ggplot(data = Visual2_Data,映射= aes(x =违反,y = ..prop ..,组= 1,填充=违反))+geom_bar()+facet_grid(〜driver_race)+scale_fill_manual(值= c(红色",绿色",黄色",蓝色",粉红色",紫色"))+主题(axis.text.x = element_text(angle = 90)) 

我的结果如下:

尽管使用 fill scale_fill_manual ,这些条仍然是默认颜色.

我将 .. count .. 用作y变量而不是 .prop .. 并删除 group = 1 :

  v2<-ggplot(data = Visual2_Data,映射= aes(x =违反,y = ..count ..,fill =违反))+geom_bar()+facet_grid(〜driver_race)+scale_fill_manual(值= c(红色",绿色",黄色",蓝色",粉红色",紫色"))+主题(axis.text.x = element_text(angle = 90)) 

我得到以下信息:

这就是我想要的,除了我想在第一张图中使用 y = ..prop .. group = 1 来具有这些颜色使用 y = ..counts .. 的过程.那么有办法吗?

预先感谢

为重现性:

我必须注意,这是一个相对较大的数据集.

我处理来自此来源的Colorado数据:


或者,我总是喜欢预先进行预处理.您可以执行以下操作:

 库(data.table)df<-setDT(copy(diamonds))[,..(N = .N),by =.(切割,颜色)] [,.(prop = N/sum(N),color = color),通过=切]ggplot(data = df,映射= aes(x =颜色,y =道具,填充=颜色))+geom_col()+facet_grid(〜cut)+scale_fill_manual(值= c(红色",绿色",黄色",蓝色",粉红色",紫色",黑色"))+主题(axis.text.x = element_text(angle = 90)) 

I'm trying to color a bar plot on ggplot that shows the proportions. I call the plot using:

v2 <- ggplot(data = Visual2_Data, 
         mapping = aes(x = violation, y = ..prop.., group = 1, fill = violation)) +
      geom_bar() +
      facet_grid(~driver_race) +
      scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple"))+
      theme(axis.text.x = element_text(angle=90))

and my result is the following:

Despite using the fill and scale_fill_manual, the bars are still the default color.

As soon as I use ..count.. as y variable instead of ..prop.. and delete group = 1 :

v2 <- ggplot(data = Visual2_Data, 
         mapping = aes(x = violation, y = ..count.., fill = violation)) +
      geom_bar() +
      facet_grid(~driver_race) +
      scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple"))+
      theme(axis.text.x = element_text(angle=90))

I get the following:

and this is what I want, except I would like to have these colors in the first plot using y = ..prop.. and group = 1, instead of using y = ..counts... So is there a way to do this?

Thanks in advance

For reproducibility:

I have to note that it is a relatively big data set.

I work with the Colorado data from this source:

https://openpolicing.stanford.edu/data/

I tidied it a bit:

data <- read_csv() #insert data here

Visual2_Data <- data %>%
  subset(out_of_state == FALSE) %>%
  select(county_name, county_fips, police_department, driver_gender,          
         driver_age, driver_race, violation, search_conducted, 
         contraband_found, stop_outcome, is_arrested) %>%
  drop_na(county_name) %>%
  filter(driver_race != "Other",
         violation %in% c("Lights", "Speeding", "Safe movement", "License", 
                          "Seat belt", "Registration/plates"))

# After this I used the code for v2 which already is described above.

v2 <- ggplot(data = Visual2_Data, etcetera)

解决方案

If you replace the fill = ... with fill = factor(..x..) you get the desired result:

  ggplot(diamonds, aes(x = color, y = ..prop.., fill = factor(..x..), group = 1)) +
    geom_bar() +
    facet_grid(~cut)+ 
    scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple", "black")) + 
    theme(axis.text.x = element_text(angle=90))


Alternatively, I always like to do my pre-processing beforehand. You could do this with:

 library(data.table)
  df <- setDT(copy(diamonds))[, .(N = .N), by = .(cut, color)][, .(prop = N/sum(N), color = color), by = cut]

  ggplot(data = df, 
             mapping = aes(x = color, y = prop, fill = color)) +
  geom_col() +
  facet_grid(~cut) +
  scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple", "black"))+
  theme(axis.text.x = element_text(angle=90))

这篇关于在ggplot中使用..prop ..时如何绘制彩条图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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