【问题标题】:How can you manually adjust gradient scale for tile plot in R?如何手动调整 R 中平铺图的渐变比例?
【发布时间】:2020-05-18 17:14:16
【问题描述】:

此图块显示了德克萨斯州人口最多的十个县的冠状病毒感染率强度。最浅红色的瓷砖在当天有 0% 的县人口确诊冠状病毒病例。最深红色的瓷砖在当天有 0.25% 的县人口确诊冠状病毒病例。

是否可以调整比例以将最深的红色设置为 50%?源数据集中没有这样的值,只是认为如果我们可以将最高强度颜色设置为数据集中最大值以外的值,它可能更符合数据可视化设计原则。

例如,将最深的红色设置为西班牙流感期间的感染率峰值可能会很有趣。

谢谢!

library("tidyverse")
library("ggthemes")
library("RColorBrewer")

# may16 is a modified dataset from the Texas Department of State Health Services: (1) it's been transposed from wide to long, (2) it's been mutated to include a new "Rate" column, (3) "Date" column has been transformed from type <chr> to <date>, and (4) it's .csv instead of .xlsx.
# may16 modified dataset includes 18,035 rows and 5 columns: (1) "County" <chr>, (2) "Population" <int>, (3) "Date" <date>, (4) "Cases" <int>, and (5) "Rate" <dbl>
# top10 is a list of the names of the ten most populous counties in Texas.

may16 %>% filter(County %in% top10)
%>% ggplot(aes(Date, County, fill=Rate))
+ geom_tile(color="grey50")
+ theme_minimal()
+ theme(panel.grid=element_blank())
+ scale_fill_gradientn(colors=RColorBrewer::brewer.pal(9, "Reds"))
+ geom_vline(xintercept=as.Date("2020-05-01"))
+ labs(title = "Coronavirus Infection Rates", subtitle = "for 10 most populous counties", caption = "Source: Texas Department of State Health Services 2020", x="", y="")
+ geom_text(aes(as.Date("2020-04-25"),"Travis",label="Texas re-opens »"))

这可能与 scale_fill_gradientn() 函数有关,但我尝试的最多转换只是将较深的红色应用于数据集中的更多值,这与我的情况相反正在寻找。

谢谢!

【问题讨论】:

    标签: r dataset data-visualization gradient legend-properties


    【解决方案1】:
    scale_fill_gradientn(limits = c(-3,3)
    

    在本例中,“-3”表示下限,“3”表示上限。

    这将覆盖基于源数据集的默认下限和上限。

    ggplot scale color gradient to range outside of data range

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 1970-01-01
      • 2017-07-22
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2019-08-09
      • 1970-01-01
      相关资源
      最近更新 更多