如何在GGparcoord R中的平行坐标图中添加刻度线?

时间:2019-09-04 11:48:55

标签: r ggplot2 graph-visualization

我正在尝试为平行坐标图中的每个点添加标签(如下所示)。但是,ggplot2中的标签不是文本,而是带数字(图形中为红色)。在下面的数据示例中,“状态”列用作图形中每个点的标签。我该怎么办?

这是数据集的样本。

States     cases      vacancy
AP           20          14
Punjab       45          67
Gujarat      15          45
Rajasthan    10          5

这是我的代码:

require(dplyr)
library(GGally)
library(ggplot2)

df = data

p <- ggparcoord(df, columns=c(2:3), groupColumn = 4, showPoints = TRUE, title = "Vacancy vs cases",
           alphaLines = 0.3) +
  theme(panel.grid.major.x=element_line(colour="grey70"))

p <- p + theme(
  # Remove panel border
  panel.border = element_blank(),  
  # Remove panel grid lines
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  # Remove panel background
  panel.background = element_blank(),
  # Add axis line
  axis.line = element_line(colour = "grey")
)

p <-p+ theme_void() 

p <- p+ geom_line()+ geom_text(aes(label = States, colour = "Grey"))

p

这是图

enter image description here

1 个答案:

答案 0 :(得分:1)

也许这可以帮助:

    library(GGally)
    library(ggplot2)
        # you'd add the right groupColumn:
    p <- ggparcoord(df, columns=c(2:3),groupColumn = 1,  showPoints = TRUE, title = "Vacancy vs cases",
                alphaLines = 0.3, mapping=aes(color="black")) +
    theme(panel.grid.major.x=element_line())   + 
  theme(
   panel.border = element_blank(),  
   panel.grid.major = element_blank(),
   panel.grid.minor = element_blank(),
   panel.background = element_blank(),
   axis.line = element_line()
       ) +
   theme_void() + 
   geom_line() + geom_text(aes(label = States), color = 'black') + guides(color = FALSE, size = FALSE)

    p

enter image description here


有数据:

data <- read.table(text ="States     cases      vacancy
AP           20          14
Punjab       45          67
Gujarat      15          45
Rajasthan    10          5",header = T)