In pacakage GGally
ggparcoord
, I want add labels to the plot, but geom_text
failed, how to fix it ? Thanks!
library(GGally)
library(tidyverse)
data <- iris
plot_basic
plot_basic <- data %>%
arrange(desc(Species)) %>%
group_by(Species) %>%
slice(1:3) %>%
ggparcoord(
columns = 1:4, groupColumn = 5, order = "anyClass",
showPoints = TRUE,
title = "Original",
alphaLines = 1
) +
scale_color_manual(values=c( "#69b3a2", "red", "darkblue") ) +
theme(
plot.title = element_text(size=10)
) +
xlab("")
label_data
for adding labelslabel_data <- cbind(ggplot_build(plot_basic)$data[[1]],
data %>%
arrange(desc(Species)) %>%
group_by(Species) %>% slice(1:3) %>% pivot_longer(-Species))
geom_text
add labels to plot_basic
, but failed . How to fix it ?plot_basic +
geom_text(data = label_data,aes(x = x,y =y ,label=value))
As the error message you should get is telling you, there is no column .ID
in the data used for the labels. To fix that, add inherit.aes=FALSE
. However, after a closer look I think simply binding to the layer data from ggplot_build
will not work as the order of the datasets differ.
Instead I would suggest to bind only the values you want as label to the plot data:
plot_basic$data <- plot_basic$data |>
bind_cols(label_data |> ungroup() |> select(label = value))
plot_basic +
ggrepel::geom_text_repel(
aes(label = label)
)