繁体   English   中英

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

[英]ggplot/GGally - Parallel Coordinates - y-axis labels

有谁知道是否有办法在GGally中为ggparcoord函数添加变量标签? 我用geom_text尝试了很多方法,但没有任何结果。

为了更加明确,我期待通过row.names(mtcars)通过geom_text 我可以区分汽车的唯一方法是通过groupColumn参数传递row.names(mtcars) ,但我不喜欢它的外观。

不起作用:

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

解决方案1

解决方案2 :此替代方法使用carName作为组列,并且不将其作为并行坐标列之一传递。 (我认为这可能更接近此函数预期的用例...)将carName指定为group列允许在ggparcoord()创建的ggplot对象的data槽中捕获car name值,所以我们的geom_text标签可以直接继承它,甚至只过滤对应于variable == "mpg" (或者在实际用例中命名第一个并行坐标列的行)。 该y坐标都没有均匀地分布同上,但geom_text_repel从ggrepel包确实在转变重叠的文本标签彼此远离一份体面的工作。

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

解决方案2

解决方案3/4 :您实际上可以使用ggplot()进行相同的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

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

合并的地块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM