r - ggplot 使图例符号变细

标签 r ggplot2

我试图从 BBC 复制这个数字。我很接近,但我正在努力使图例符号变薄。这可能吗?

enter image description here

library(tidyverse)
library("rio")
url <- "https://gist.githubusercontent.com/ericpgreen/a728be304b811fe7708699682eb4ba42/raw/fd924596e30c131dbaf97c00c9d9863bb40abb9a/bbcCovid.R"
df_plot <- rio::import(url)

ggplot(df_plot, aes(x=date, 
                    y=reorder(Country.Region, 
                              total, 
                              order=TRUE))) +
  geom_tile(aes(fill=casesRollf), 
            color="white", 
            na.rm = TRUE
            #, key_glyph = draw_key_timeseries
  ) +
  theme_bw() + theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        plot.title.position = "plot") +
  scale_x_date(breaks = as.Date(c("2020-02-14",
                                  "2020-03-05",
                                  "2020-03-25",
                                  "2020-04-14")),
               date_labels = "%d %b") +
  scale_fill_manual(values=
                      c("#e4e4e4", "#ffeed2", 
                        "#ffda64", "#faab19", 
                        "#d2700d",  
                        "#d56666", "#9a1200", 
                        "#5b0600", "#000000"),
                    guide = guide_legend(reverse = TRUE),
                    labels=c("No cases", "1 to 10",
                             "11 to 50", "51 to 100",
                             "101 to 250", "251 to 500",
                             "501 to 1,000", "1,001 to 5,000",
                             "> 5,000")) +
  labs(title = "Where are the most new coronavirus cases?",
       subtitle = "New confirmed cases, three-day rolling average",
       x="",
       y="")

enter image description here

最佳答案

您可以使用 legend.key.widththeme .为了更接近原始图,您可以使用 legend.positionplot.margin如:

ggplot(df_plot, aes(x=date, 
                    y=reorder(Country.Region, 
                              total, 
                              order=TRUE))) +
  geom_tile(aes(fill=casesRollf), 
            color="white", 
            na.rm = TRUE
            #, key_glyph = draw_key_timeseries
  ) +
  theme_bw() + theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_blank()) +
  scale_x_date(breaks = as.Date(c("2020-02-14",
                                  "2020-03-05",
                                  "2020-03-25",
                                  "2020-04-14")),
               date_labels = "%d %b") +
  scale_fill_manual(values=
                      c("#e4e4e4", "#ffeed2", 
                        "#ffda64", "#faab19", 
                        "#d2700d",  
                        "#d56666", "#9a1200", 
                        "#5b0600", "#000000"),
                    guide = guide_legend(reverse = TRUE),
                    labels=c("No cases", "1 to 10",
                             "11 to 50", "51 to 100",
                             "101 to 250", "251 to 500",
                             "501 to 1,000", "1,001 to 5,000",
                             "> 5,000")) +
  labs(title = "Where are the most new coronavirus cases?",
       subtitle = "New confirmed cases, three-day rolling average",
       x="",
       y="")+
  theme(plot.margin=unit(c(0.25,2,0.25,0.25),"cm"),
        legend.position=c(1.03,0.72),
        legend.key.width = unit(0.4,"line"))

enter image description here

顺便说一句:不错的作品;)

关于r - ggplot 使图例符号变细,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260999/

相关文章:

r - readOGR 和 st_read 之间的区别

java - 在 "upgrading"到 OSX Yosemite 之后,RStudio/R 中的 rJava 加载错误

r - 如何在 Alluvial/Sankey 图(在 R ggalluvial 上)的流量项上添加值标签?

r - 将每 4 行转置为 4 个单独的列

r - 如何根据位数将数值转换为小时和分钟

r - 使用 data.table R 在滚动基础(重置和恢复)中提取累积唯一值

r - 如何根据 ggplot2 中的第二个变量更改具有相同形状的点子集的内部颜色(填充)?

R:在堆栈栏上显示字符或类别

r - 使用 ggplot 中的另一个变量创建一个填充的直方图

r - R 和 CI 中的单向方差分析图