【问题标题】:Rowwise coloring in facet_grid in ggplotggplot中facet_grid中的逐行着色
【发布时间】:2020-02-15 14:24:08
【问题描述】:

我创建了一个圆形网格,每行显示一个州在 7 个基于圆形大小的不同参数上的地位。这些参数以百分比表示,每个状态加起来为 100。对于我的 facet_grid,我想根据各个状态的比例为每个圆圈着色和调整大小,而不是根据整个网格。我会用一个例子来详细说明。

圆形网格

library(ggforce)
library(tidyverse)
library (ggplot)
library(reshape2)

d <- data.frame("state" = c("Alabama", "Florida", "Washington", "New York"),
       "A" = c(20, 25, 45, 11),
       "B" = c(25, 25, 10, 4),
       "C" = c(50, 5, 20, 5),
       "D" = c(5, 45, 25, 80))

d1 <- melt(d, id.vars = "state")

p3 <- ggplot(d1) +
  geom_circle(aes(x0 = 0.5, y0 = 0.5, r = value, color = "white", fill = value), data = d1) +
  scale_fill_continuous(low = "#5fc5bd", high = "#f37021") +
  scale_color_manual(values = "white") +
  coord_fixed() +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank() ,
        legend.position = "none",
        panel.border = element_blank(), 
           axis.title.x=element_blank(),
           axis.title.y=element_blank(),
        axis.text = element_blank())

p3 <- p3 + facet_grid(state ~ variable, labeller=label_wrap_gen(width=10)) +
  theme(strip.text.x = element_text(size = 10, face = "bold"),
          strip.text.y = element_text(size = 10, face = "bold"),
          strip.background = element_rect(colour = "gray", fill = "white"))
p3

到目前为止,图表上的圆圈正在根据整个数据集中的最大值进行缩放。我希望它考虑每个州的值并根据每个州的值缩放圆圈大小 - 所以阿拉巴马州最大的圆圈应该是 C,颜色为橙色(该州的最高值),其他州也是如此。任何想法都会非常有帮助,谢谢。

【问题讨论】:

    标签: r ggplot2 data-visualization facet-grid


    【解决方案1】:

    您可以向 df1 添加一个新列来实现此目的:

    d1 <- melt(d, id.vars = "state") %>%
          group_by(state) %>%
          mutate(per_state = value/max(value))
    

    现在你设置 r = per_statefill = per_state 来得到这个:

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 2021-06-24
      • 2013-10-25
      • 2021-09-05
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多