首页 > 解决方案 > 如何在 R 中创建类似于附加图像的视觉效果?

问题描述

我希望展示从 2017 年到 2019 年队列数量变化之间的差异,我遇到了一个图表,该图表是从我想在 R 中创建的演示文稿中发送的,因为我觉得这将是展示我的最佳视觉效果数据。

我尝试使用当前站点中的示例数据从以下 URL 创建斜率图:

http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Slope%20Chart

这给了我图表内的 2 轴,但它现在试图了解我如何在两者之间添加颜色以使其看起来像附加的图像。我还查看了面积图的工作原理,我查看的 URL 之一如下:

https://www.displayr.com/how-to-make-an-area-chart-in-r/

因为我所拥有的图像似乎是面积图和斜率图的混合体,所以我想我可以尝试看看面积图中是否有任何关于如何填充颜色的代码,但这仅向我展示了它使用的属性来填补差异。

当我从网站复制数据时,我使用的代码来自上述 URL ( http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Slope%20Chart ),但是我一直在努力进行任何更改以使其看起来像所附图像。

如果有人能建议我是否走正确的路线,或者我所附的图表可以使用 R 中的包创建,我将非常感激,因为我以前没有遇到或看到过这种视觉效果。将不胜感激我能得到的任何支持。

我试图复制的图表

我创建的代码(全部归功于http://r-statistics.co/

library(ggplot2)
library(scales)
theme_set(theme_classic())

# prep data
df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv")
colnames(df) <- c("continent", "1952", "1957")
left_label <- paste(df$continent, round(df$`1952`),sep=", ")
right_label <- paste(df$continent, round(df$`1957`),sep=", ")
df$class <- ifelse((df$`1957` - df$`1952`) < 0, "red", "green")

# Plot
p <- ggplot(df) + geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1) +
                  scale_color_manual(labels = c("Up", "Down"), 
                                     values = c("green"="#00ba38", "red"="#f8766d")) +  # color of lines
                  labs(x="", y="Mean GdpPerCap") +  # Axis labels
                  xlim(.5, 2.5) + ylim(0,(1.1*(max(df$`1952`, df$`1957`))))  # X and Y axis limits

# Add texts
p <- p + geom_text(label=left_label, y=df$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=df$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Time 1", x=1, y=1.1*(max(df$`1952`, df$`1957`)), hjust=1.2, size=5)  # title
p <- p + geom_text(label="Time 2", x=2, y=1.1*(max(df$`1952`, df$`1957`)), hjust=-0.1, size=5)  # title

# Minify theme
p + theme(panel.background = element_blank(), 
           panel.grid = element_blank(),
           axis.ticks = element_blank(),
           axis.text.x = element_blank(),
           panel.border = element_blank(),
           plot.margin = unit(c(1,2,1,2), "cm"))

结果:

斜率图

标签: rdata-visualization

解决方案


我认为做到这一点的最好方法是geom_polygon在每个时间点累积 gdp 总和的预先构建的数据帧上使用:

zero_row <- tibble(continent = "0start",  # creates a row of 0's
                   year = c("1952", "1957"),
                   class  ="none",
                   gdp = 0)


df2 <- df %>% 
  gather("year", "gdp", 2:3) %>%  # separate years to new rows
  bind_rows(zero_row, .) %>% 
  group_by(year) %>% 
  mutate(y2 = cumsum(gdp)) %>%  # cumulative sum to give top y values
  arrange(continent, year)

df2[3:12, "y1"] <- df2[1:10, "y2"]  # insert bottom y values

order_df <- tibble(year = c("1952", "1952", "1957", "1957"),
                   level = c("y1", "y2", "y2", "y1"), 
                   order = 1:4)  # order to plot


p <- df2[3:12,] %>% 
  gather("level", "y", 5:6) %>%  # separate y values to new rows
  full_join(order_df, by = c("year", "level")) %>% 
  arrange(continent, order) %>% 
  ggplot() + 
  geom_polygon(aes(year, y, fill = continent, group = continent)) +
  geom_vline(xintercept=1, linetype="dashed", size=.1) +
  geom_vline(xintercept=2, linetype="dashed", size=.1) +
  labs(x="", y="Mean GdpPerCap") +
  scale_y_continuous(limits = c(0, 1.2*max(df2$y2)), expand = c(0 ,0))

lab_pos <- df2 %>%  # new label positions
  filter(continent !="0start") %>% 
  select(continent, year, y2) %>% 
  spread(year, y2)

# Add texts
p <- p + geom_text(data = df, label=left_label, y = lab_pos$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
p <- p + geom_text(data = df, label=right_label, y=lab_pos$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Time 1", x=1, y=1.1*(max(df2$`y2`)), hjust=1.2, size=5)  # title
p <- p + geom_text(label="Time 2", x=2, y=1.1*(max(df2$`y2`)), hjust=-0.1, size=5)  # title

# Minify theme
p + theme(panel.background = element_blank(), 
          panel.grid = element_blank(),
          axis.ticks = element_blank(),
          axis.text.x = element_blank(),
          panel.border = element_blank(),
          plot.margin = unit(c(1,2,1,2), "cm"),
          legend.position = "none")

这将产生以下图: 在此处输入图像描述

希望有帮助!

编辑: 哎呀,忘记了该geom_area功能也存在。试试这个开始:

df2 <- df %>% 
  gather("year", "val", 2:3) 


df2 %>% 
  mutate(year = as.numeric(year),
         continent = factor(df2$continent, levels = rev(unique(df2$continent)))) %>% 
  ggplot() +
  geom_area(aes(x = year, y = val, fill = continent)) +
  scale_x_continuous(breaks = c(1952, 1957), expand = c(0.5,0.5))

(在这个中,year必须转换为数值,因此映射其余标签等需要更改。)


推荐阅读