question regarding the labeling of points on graphs generated by ggplot in R
1
0
Entering edit mode
13 months ago

I generated a kind of dummy 'spaghetti plot' in R :

df <- data.frame(Id=c("A","B","C"), condition1=c(2,3,2.5), condition2=c(5,1,2.5))

library(tidyr)
df2 <- df |> pivot_longer(cols = colnames(df[,2:3]),
                          names_to = 'condition',
                          values_to = 'n')

ggplot(df2, aes(x = condition, y = n)) +
  geom_line(aes(group=Id)) +
  geom_point()

enter image description here

Now, I'd like to highlight with colors on the graph the behavior of the Ids between condition 1 and condition 2. For example, showing an increase in red and a decrease in green.

More precisely, I'd like to label for instance the line joining the points "(A,condition1)=2" to "(A,condition2)=5" in red (increase) and the line joining the points "(B,condition1)=3" to "(B,condition2)=1" in green (decrease).

I also would like to only label the condition2 points with the same color as the line, as well as adding the Id of the condition2 points always with the same color as the line. It means that the "(A,condition2)=5" point will be red, accompagnied by an "A" red label at its side. Idem for the "(B,condition2)=1" point which will be green accompagnied with an "B" green label at its side.

Bonus (cherry in the cake) will be to add a kind of arrow next to the condition2 points. The arrow will be red if there has been an increase between condition 1 and 2 for a given Id.

Do you have any ideas?

Thanks guys!

Francois

R ggplot • 1.3k views
ADD COMMENT
1
Entering edit mode

To conditionally color the line, create a new column that indicates whether the line is increasing or decreasing. Suppose you name this new column change, you can then modify your code as follows: geom_line(aes(group=Id, color=change)).

For the arrow question can you expand on what you mean?

ADD REPLY
0
Entering edit mode

As adding a text corresponding to one point, I wanted to add a small arrow, showing the trend of the line (arrow up -> increase of the line, arrow down -> decrease of the line)

ADD REPLY
0
Entering edit mode

Do you know how to associate the color of the line to the color of the label (with geom_label_repel for instance) ?

ADD REPLY
1
Entering edit mode

You can pass the same column to to the label geom, geom_label_repel(aes(color=change)).

ADD REPLY
0
Entering edit mode

i just added "label = Id" before "color", and it work pretty well, thanks !

ADD REPLY
0
Entering edit mode

color=change gives me a continous range of colors, but I need discrete colors

ADD REPLY
1
Entering edit mode

What code are you using to create the new column? The result should be a character or factor to put it on a discrete color scale.

ADD REPLY
0
Entering edit mode

I wrote : df$change <- as.character(df$condition2 - df$condition1) instead of what I was writing before : df$change <- df$condition2 - df$condition1 and it works perfectly ! Thanks !

ADD REPLY
0
Entering edit mode

How to associate a color to a "change" value. For instance, if I want that a "change" value of 3 corresponds to a red line ?

ADD REPLY
1
Entering edit mode

You might want to instead do.

library("dplyr")

df$change <- case_when(
  df$condition2 - df$condition1 > 0 ~ "increase",
  df$condition2 - df$condition1 < 0 ~ "decrease",
  df$condition2 - df$condition1 == 0 ~ "n.c."
)

You can then manually specify the colors using scale_color_manual.

ggplot(...) + scale_color_manual(values=c("increase"="green", "decrease"="red", "n.c."="grey"))
ADD REPLY
1
Entering edit mode
13 months ago
barslmn ★ 2.3k

I'd like to label for instance the line joining the points "(A,condition1)=2" to "(A,condition2)=5" in red (increase) and the line joining the points "(B,condition1)=3" to "(B,condition2)=1" in green (decrease).

You should do the logic before plotting. Create a new column indicating wheter there is a increase or decrease and use the new column as the color variable.

Bonus (cherry in the cake) will be to add a kind of arrow next to the condition2 points. The arrow will be red if there has been an increase between condition 1 and 2 for a given Id.

You can use annotate to draw arrows. Again create your color column beforehand.

Example:

https://colab.research.google.com/drive/1p0OCRTsH8ESYcHfvOJOvGc7mG5BVe9u6?usp=sharing

ADD COMMENT

Login before adding your answer.

Traffic: 2058 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6