【问题标题】:How to add ticks in parallel coordinates graph in GGparcoord R?如何在 GGparcoord R 的平行坐标图中添加刻度?
【发布时间】:2020-01-07 07:27:10
【问题描述】:

我正在尝试为平行坐标图中的每个点添加标签(附在下面)。但是,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

这是图表

【问题讨论】:

    标签: r ggplot2 graph-visualization


    【解决方案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
    


    有数据:

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

    【讨论】:

    • 谢谢@s_t。但是,我不想按州着色。我只想要线条中的一种颜色。我该怎么做?
    • @ShreyaAgarwal 我找到了一种解决方法,标签的颜色是可自定义的,线条颜色只有红色,因为它是作为常量传递的。请参阅编辑后的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多