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.

1

2 Answers 2

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() 
Sign up to request clarification or add additional context in comments.

1 Comment

Development on gather() is complete, and for new code we recommend switching to pivot_longer(). <tidyr.tidyverse.org/reference/gather.html>
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.