解决方案 1:如果您想坚持最初的尝试,您可以计算汽车名称的适当 y 坐标,并将其添加为单独的数据源。使用inherit.aes = FALSE
以便此 geom_text 图层不会从使用创建的 ggplot 对象继承任何内容ggparcoord()
:
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 指定为组列允许在data
此时创建的 ggplot 对象的插槽中捕获汽车名称值ggparcoord()
,因此我们的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
解决方案 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
编辑
您也可以在右侧为上述每个添加文本标签。请注意,标签的位置可能没有很好的间隔,因为它们是根据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))