如何在R中为两个变量实现Slope图

时间:2018-04-27 10:28:32

标签: visualization data-visualization

我正在分析有多少用户使用过特定主题标签,以及他们如何为推文总数做出贡献。我的结果是:

数据:

enter image description here

  • 20.68%与#HashtagX相关的推文由20位用户创建。现在,这20个用户仅占使用标签#HashtagX的14,432个用户总数的0.001%。
  • 如果按推文数量排名前100位用户会怎样? 44%的推文是由前100名用户创建的。
  • 如果按用户数量扩展到前500名用户,我们会看到72%的推文是由前500名创建的。

我想知道如何实现斜率图,因为我认为这是显示两个变量之间关系的好方法,但它不是任何库提供的默认图形。

1 个答案:

答案 0 :(得分:-1)

显示两个变量之间关系的方法之一("用户" vs"推文")是斜率图。

获得可视化(解决问题的图表):

Slope Chart

1)图书馆

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

2)数据示例

Country = c('20 accounts', '50 accounts', '100 accounts','200    accounts','300 accounts',
        '500 accounts','1000 accounts','14.443 accounts')
January = c(0.14, 0.34, 0.69,1.38,2.07,3.46,6.92,100)
April = c(20.68, 33.61, 44.94, 57.49,64.11,72,80,100)
Tweets_N = c(26797, 43547, 58211, 74472,83052,93259,103898,129529)


a = data.frame(Country, January, April)
left_label <- paste(a$Country, paste0(a$January,"%"),sep=" | ")
right_label <- paste(paste0(round(a$April),"%"),paste0(Tweets_N," tweets"),sep=" | ")
a$color_class <- "green"

3)绘图

 p <- ggplot(a) + geom_segment(aes(x=1, xend=2, y=January, yend=April, col=color_class), size=.25, 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("blue", "red")) +
 labs(
  x="", y = "Percentage") +
  xlim(.5, 2.5) + ylim(0,(1.1*(max(a$January, a$April))))  # X and Y axis limits

  # Add texts
  p <- p + geom_text_repel(label=left_label, y=a$January, x=rep(1,  NROW(a)), hjust=1.1, size=3.5,direction = "y")
  p <- p + geom_text(label=right_label, y=a$April, x=rep(2, NROW(a)), hjust=-0.1, size=3.5)
 p <- p + geom_text(label="Accounts", x=1, y=1.1*(max(a$January, a$April)), hjust=1.2, size=4, check_overlap = TRUE)  # title
 p <- p + geom_text(label="Tweeets (% of Total)", x=2, y=1.1*(max(a$January, a$April)), hjust=-0.1, size=4, check_overlap = TRUE)
 # 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"))