如何在 R 中为图形生成更好的可视化效果

问题描述 投票:0回答:1
install.packages("ggplot2") 
install.packages("tidyr") 
library(ggplot2) 
library(tidyr) 

data <- data.frame(Time = c('2014', '2017'), setor = c('MI2','MI3','MI4','MI5','MI6','MI7','MI8','MI9','MI10','MI11','MI12','MI13','MI14','MI15','MI16','MI17','MI18','MI19','MI20','MI21','MI22','MI23','MI24','MI25','MI26','MI27','MI28','MI29','MI30','MI31','N2','N3','N4','N5','N6','N7','N8','N9','N10','N11','N12','N13','N14','N15','N16','N17','N18','N19','N20','N21','N22','N23','N24','N25','N26','N27','N28','N29','N30','N31'), 
Group1_Rank_2014 = c(1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,3,2,2,4,4,1,1,1,1,5,2,4,4,2,4,4,2,4,2,1,4,4,2,2,1,2,4,2,4,1), 
Group2_Rank_2017 = (1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,3,1,2,3,2,1,1,1,1,4,1,2,2,1,3,2,1,5,2,1,2,2,2,1,1,1,2,1,3,1)) 

data_long <- pivot_longer(data, cols = starts_with("Group"), names_to = "Group", values_to = "Rank") 

ggplot(data_long, aes(x = Time, y = Rank, color = "black", group = interaction(setor, Group))) + geom_line(aes(linetype = setor)) + 
 + geom_point() + labs(x = "Time", y = "Ranking", title = "Mudança de Ranking dos Setores de 2014 a 2017") + scale_y_reverse() +
   theme_minimal() + theme())

此代码不会生成一行来显示 2014 年和 2017 年之间的变化(结果显示在图像中)。但是,我需要 60 行从 2014 年的位置 1、2、3、4 或 5 到 2017 年的位置 1、2、3、4 或 5。我该如何制作呢?我需要什么:有一个图表,其中的线条代表 60 个扇区,从 2014 年的位置 1、2、3、4 或 5 到 2017 年的 1、2、3、4 或 5(X 轴必须只是 2014 年和 2017 年)

r ggplot2 graph ranking
1个回答
0
投票

我没有找到一种方法可以在单个图表中正确显示所有这些内容,因为有太多重叠,但您可以通过以下两种方法来实现它:使用 ggrepl 直接标记以将标签分开或抖动 geom_textpath:

library(tidyverse)
library(ggrepel)
library(geomtextpath)

data <- data.frame(
  sector = c('MI2','MI3','MI4','MI5','MI6','MI7','MI8','MI9','MI10','MI11','MI12','MI13','MI14','MI15','MI16','MI17','MI18','MI19','MI20','MI21','MI22','MI23','MI24','MI25','MI26','MI27','MI28','MI29','MI30','MI31','N2','N3','N4','N5','N6','N7','N8','N9','N10','N11','N12','N13','N14','N15','N16','N17','N18','N19','N20','N21','N22','N23','N24','N25','N26','N27','N28','N29','N30','N31'),
  group1_rank_2014 = c(1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,3,2,2,4,4,1,1,1,1,5,2,4,4,2,4,4,2,4,2,1,4,4,2,2,1,2,4,2,4,1), 
  group2_rank_2017 = c(1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,3,1,2,3,2,1,1,1,1,4,1,2,2,1,3,2,1,5,2,1,2,2,2,1,1,1,2,1,3,1)
  ) 

data_long <- pivot_longer(
  data %>% filter(group1_rank_2014 != group2_rank_2017), 
  cols = starts_with("group"), 
  names_to = "group", 
  values_to = "rank") %>% 
  mutate(time = substr(group, nchar(group) - 3, nchar(group)),
         time = factor(time, levels = c("2014", "2017"))
  )

ggplot(data_long, aes(x = time, y = rank)) +
  geom_line(
    aes(group = sector), 
    ) +
  geom_text_repel(data = data_long %>% filter(time == "2014"), 
            aes(label = sector, x = "2014", y = rank), hjust = 1, size = 3,
            max.overlaps = Inf, direction = "y", min.segment.length = 10,
            force = 1.5
  ) +
  geom_text_repel(data = data_long %>% filter(time == "2017"), 
            aes(label = sector, x = "2017", y = rank), hjust = 0, size = 3,
            max.overlaps = Inf, direction = "y", min.segment.length = 10,
            force = 1.5
  ) +
  theme_minimal()



ggplot(data_long, aes(x = time, y = rank)) +
  geom_textpath(
    aes(group = sector, label = sector), 
    size = 3,
    position = position_jitter(width = 0, height = 0.2, seed = 1)
  ) +
  theme_minimal()
© www.soinside.com 2019 - 2024. All rights reserved.