有没有人知道是否有办法在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))
任何想法都将不胜感激!
答案 0 :(得分:2)
解决方案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
解决方案2 :该替代方法使用carName作为组列,并且不将其作为平行坐标列之一传递。 (我认为这可能更接近于此功能预期的用例...)将carName指定为group列可将汽车名称值捕获在{{创建的ggplot对象的data
}插槽中1}},因此我们的ggparcoord()
标签可以直接继承它,甚至仅过滤对应于geom_text
的行(或在实际使用情况下命名的任何平行坐标列的第一行) )。 y坐标没有像上面那样均匀分布,但是ggrepel软件包中的variable == "mpg"
在将重叠的文本标签彼此移开方面做得不错。
geom_text_repel
解决方案3/4 :您实际上可以使用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
绘制相同内容,而不必依赖于可能在幕后做意外事情的扩展程序:
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
的缩放值进行定位的:
wt