r - ggplot/GGally-平行坐标-y轴标签

标签 r ggplot2 data-visualization parallel-coordinates

有谁知道是否可以在GGally中将变量标签添加到ggparcoord函数中?我已经尝试了geom_text的多种方法,但是没有任何结果。

更明确地说,我希望通过row.names(mtcars)传递geom_text。我唯一可以区分汽车的方法是通过row.names(mtcars)参数传递groupColumn,但我不喜欢这种外观。

不起作用:

mtcars$carName <- row.names(mtcars) # This becomes column 12
library(GGally)
# Attempt 1
ggparcoord(mtcars, 
           columns = c(12, 1, 6), 
           groupColumn = 1) +
geom_text(aes(label = carName))

# Attempt 2
ggparcoord(mtcars, 
           columns = c(12, 1, 6),
           groupColumn = 1,
           mapping = aes(label = carName))

任何想法,将不胜感激!

最佳答案

解决方案1 ​​:如果您想坚持原始尝试,则可以为汽车名称计算适当的y坐标,并将其添加为单独的数据源。使用inherit.aes = FALSE,以便此geom_text层不会继承使用ggparcoord()创建的ggplot对象的任何内容:

library(dplyr)

p1 <- ggparcoord(mtcars, 
                 columns = c(12, 1, 6), 
                 groupColumn = 1) +

  geom_text(data = mtcars %>%
              select(carName) %>%
              mutate(x = 1,
                     y = scale(as.integer(factor(carName)))),
            aes(x = x, y = y, label = carName),
            hjust = 1.1,
            inherit.aes = FALSE) +

  # optional: remove "carName" from x-axis labels
  scale_x_discrete(labels = function(x) c("", x[-1])) + 

  # also optional: hide legend, which doesn't really seem relevant here
  theme(legend.position = "none")
p1

solution 1

解决方案2 :此替代方法使用carName作为组列,并且不将其作为平行坐标列之一传递。 (我认为这可能更接近于此功能预期的用例...)将carName指定为group列可允许将汽车名称值捕获在data所创建的ggplot对象的ggparcoord()插槽中,因此,我们这次geom_text标签可以直接继承它,甚至只过滤与variable == "mpg"相对应的行(或在实际使用情况下,首先命名任何平行坐标列)。 y坐标没有像上面那样均匀分布,但是ggrepel包中的geom_text_repel在将重叠的文本标签彼此移开方面做得很不错。
library(dplyr)
library(ggrepel)

p2 <- ggparcoord(mtcars, 
           columns = c(1, 6), 
           groupColumn = "carName") +
  geom_text_repel(data = . %>%
                    filter(variable == "mpg"),
                  aes(x = variable, y = value, label = carName),
                  xlim = c(NA, 1)) + # limit repel region to the left of the 1st column
  theme(legend.position = "none") # as before, hide legend since the labels 
                                  # are already in the plot
p2

solution 2

解决方案3/4 :实际上,您可以使用ggplot()绘制相同内容,而无需依赖于可能在幕后做意外事情的扩展:
library(dplyr)
library(tidyr)
library(ggrepel)

# similar output to solution 1

p3 <- mtcars %>%
  select(carName, mpg, wt) %>%
  mutate(carName.column = as.integer(factor(carName))) %>%
  gather(variable, value, -carName) %>%
  group_by(variable) %>%
  mutate(value = scale(value)) %>%
  ungroup() %>%

  ggplot(aes(x = variable, y = value, label = carName, group = carName)) +
  geom_line() +
  geom_text(data = . %>% filter(variable == "carName.column"),
            hjust = 1.1) +
  scale_x_discrete(labels = function(x) c("", x[-1]))
p3

# similar output to solution 2

p4 <- mtcars %>%
  select(carName, mpg, wt) %>%
  gather(variable, value, -carName) %>%
  group_by(variable) %>%
  mutate(value = scale(value)) %>%
  ungroup() %>%

  ggplot(aes(x = variable, y = value, label = carName, group = carName)) +
  geom_line() +
  geom_text_repel(data = . %>% filter(variable == "mpg"),
                  xlim = c(NA, 1))
p4

solutions 3 / 4

编辑

您也可以在上面的每一项中在右侧添加文本标签。请注意,标签的位置可能没有很好地隔开,因为它们是根据wt的缩放值定位的:
p1 +
  geom_text(data = mtcars %>%
              select(carName, wt) %>%
              mutate(x = 3,
                     y = scale(wt)),
            aes(x = x, y = y, label = carName),
            hjust = -0.1,
            inherit.aes = FALSE)

p2 +
  geom_text_repel(data = . %>%
                    filter(variable == "wt"),
                  aes(x = variable, y = value, label = carName),
                  xlim = c(2, NA))

p3 +
  geom_text(data = . %>% filter(variable == "wt"),
            hjust = -0.1)

p4 +
  geom_text_repel(data = . %>% filter(variable == "wt"),
                  xlim = c(2, NA))

combined plots

关于r - ggplot/GGally-平行坐标-y轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438425/

相关文章:

r - 在列表上使用lapply并添加具有数据框架名称的列

R 函数根据另一组值评估 data.table 列中的值

r - 增加 ggplot2 树状图中叶子和标签之间的空间

javascript - 在列中包含键的堆积图

javascript - ChartJS (React) 折线图 - 如何显示带有来自 3 个(多个)数据集的数据和标签的单个工具提示?

r - 在数据框中的变量中查找 n% 的记录

r - 计算R中向量中重复数字序列的长度

r - ggplot2:有没有办法将单个图覆盖到 ggplot 中的所有方面

r - 覆盖网格而不是在网格之上绘制

data-visualization - 如何更改 Graphite/Grafana 中的 x 轴(按天绘制)?