0

Can't find a way to fix this!

I want to plot a data frame which contains 4 columns it looks like this :

Species Mean for today Mean RCP26 Mean RCP85
1 0.567 0.765 0.342
2 0.987 0.543 0.001
3 0.456 0.876 0.54

Now I want to plot the col names as the x-axis labels and the corresponding values above them. The species should have a color code in the legend. In the end I hope to get a line diagramm which shows the development from today to rcp85.

I'm trying for hours now using ggplot but I cant find a way to realize it. Would be really glad to get some help.

Quercus
  • 3
  • 1
  • 3
  • 1
    Please display your data in a way that can make it easier for respondents to use. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Mouad_Seridi May 05 '21 at 09:35

2 Answers2

1

you need to pivot your data first, this is achieved by gather, also you need the group aesthetic to get a line with a discrete x axis

require(dplyr)
require(ggplot2)
    
df %>% 
gather(var, val , -Species) %>% 
ggplot(aes(x = var, y = val, color = as.factor(Species), group = as.factor(Species)))+
geom_line() 
Mouad_Seridi
  • 2,666
  • 15
  • 27
  • Development on gather() is complete, and for new code we recommend switching to pivot_longer(). – TarJae May 05 '21 at 17:32
0

I also used tidyr to pivot the table.

library(ggplot2)
library(dplyr)
library(tidyr)

df %>% 
  pivot_longer(cols = 2:4, names_to = "indicators", values_to = "values") %>% 
  ggplot(data = ., aes(x = indicators, y = values, color = as.factor(Species), group = Species)) +
    geom_point()

The output:

enter image description here

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Paul
  • 2,850
  • 1
  • 12
  • 37