如何在R中创建类似于附件图像的视觉效果?

时间:2019-07-09 08:45:56

标签: r data-visualization

我希望显示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中的一个软件包创建我所附加的图表,而我以前从未见过或看到这种视觉效果,那么我将不胜感激。希望能得到我的支持。

chart that I am trying to replicate

我创建的代码(全部归功于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"))

结果:

Slope Chart

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是在预先构造的数据帧上使用// ... .then(swReg => { console.log('SW registered'); this.setState({ swReg: swReg }); }) // ... ,并在每个时间点累积gdp的总和:

geom_polygon

这将产生以下图: enter image description here

希望有帮助!

编辑: 糟糕,忘记了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)) 必须转换为数字值,因此需要更改映射其余标签等。)