微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过两个条件填充ggplot2直方图

如何解决通过两个条件填充ggplot2直方图

我有一些数据(secret_word = "Banana" guesses = "" maximum_guesses = 5 number_of_guesses = 0 out_of_guesses = False while guesses != secret_word and not out_of_guesses: if number_of_guesses == 0: guesses = input("Guess a word: ") number_of_guesses += 1 elif number_of_guesses == 1: print("The object is edible") guesses = input("Guess again: ") number_of_guesses += 1 elif number_of_guesses == 2: print("The object is yellow: ") guesses = input("Guess again: ") number_of_guesses += 1 elif number_of_guesses == 3: print("The object is a fruit: ") guesses = input("Guess again: ") number_of_guesses += 1 elif number_of_guesses == 4: print("You're stupid ") guesses = input("Guess again: ") number_of_guesses += 1 else: out_of_guesses == True if out_of_guesses == True: print("You have lost") else: print("you have won") )要绘制为直方图。有两种样本类型(x),每种样本都是通过两种不同的方式(s)收集的。

我想将它们绘制成由rs填充的堆叠直方图-最好的方法是按照我的上一个示例对它们进行分面,但是我是空间有限。

我可以使用单个r来绘制s填充的数据。我可以绘制两个geom_histogram来绘制不同的geom_histogram。我不知道如何使不同的几何堆栈。我还没有弄清楚如何填充它们,但是ggnewscale可能是一个选择。

我想选择r ==红色,蓝色和s ==相应r颜色的浅,深的填充。

有什么建议吗?

s

library(tidyverse)
library(tibble)

x <-           c(0,1,-1,2,-2,3,-3,4,-4,8,9,7,10,6,11,5,12,4)
r <- as.factor(c(1,1))
s <- c(rep.int("a",25),rep.int("b",25))

figsd <- tibble(x,r,s)
  
figsd %>% 
ggplot(aes(x=x)) +
  geom_histogram(aes(fill = s),binwidth = 1,position = "stack") +
  coord_cartesian(ylim = c(0,5))



figsd %>% 
  ggplot(aes(x=x)) +
  geom_histogram(data = . %>% filter(r == 1),aes(fill = s),alpha = 0.5,position = "stack") +
  geom_histogram(data = . %>% filter(r != 1),5))

reprex package(v0.3.0)于2020-11-02创建

解决方法

您可以在两个分组之间创建一个交互并将其传递给fill美学:

library(ggplot2)

figsd <- data.frame(
    x = c(0,1,-1,2,-2,3,-3,4,-4,8,9,7,10,6,11,5,12,4),r = as.factor(c(1,1)),s = c(rep.int("a",25),rep.int("b",25))
)


ggplot(figsd,aes(x,fill = interaction(r,s))) +
    geom_histogram(binwidth = 1) + 
    scale_fill_manual(values = c(
        '1.a' = 'lightblue','1.b' = 'darkblue','2.a' = 'pink','2.b' = 'darkred'
    )) + 
    labs(fill = 'group')

histogram of interaction

这里的水平本质上没有任何意义,因此您可能需要故意设置填充颜色以显示关系。

还请注意,无论出于何种原因,interaction()都无法很好地扩展-如果要处理10万行,即使paste()在语义上更正确,interaction()的速度也会大大提高就您要显示的内容而言。

,

摘自@alistaire的评论。

重新排序和着色以使其更好一点。

library(tidyverse)
library(tibble)

x <-           c(0,4)
r <- as.factor(c(1,1))
s <- c(rep.int("a",25))

figsd <- tibble(x,r,s)

figsd %>%  mutate(r = factor(r,levels=c(2,1))) %>% 
ggplot(aes(x=x)) +
  geom_histogram(aes(fill = interaction(r,s)),binwidth = 1,position = "stack")+ 
  scale_fill_manual(breaks = c("1.a","2.a","1.b","2.b"),values = c('1.a' = 'lightblue','1.b' = 'pink','2.a' = 'darkblue','2.b' = 'darkred'),labels = c("1.a","2.b")
                   ) +
  coord_cartesian(ylim = c(0,5))

reprex package(v0.3.0)于2020-11-02创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。