0

I am very new to R and am having trouble filling in the points on a line graph. When i enter my code no colours show on the plots, I am not sure what i am doing wrong. This is my code so far:

Mydata1 <- read_xlsx("41586_2020_2505_MOESM3_ESM.xlsx", sheet = 2) 

Mydata1$Metabolite <- factor(Mydata1$Metabolite, 
                             levels=c("G6P", "FBP", "Malate", "Citrate"))

ggplot(Mydata1, aes(x=`Time [hr]`, y=`Relative Flux (%)`, shape=Metabolite)) + 
  geom_line(lwd=0.5) +
  geom_point(size=4) + 
  scale_shape_manual(values = c(21, 22, 23, 25)) + 
  scale_color_manual(values = c("dodgerblue4", "dodgerblue3", 
                                "dodgerblue2", "dodgerblue1")) +
  coord_cartesian(ylim=c(0, 6)) + 
  geom_hline(yintercept=1, linetype="longdash", size=1)    
dput(head(Mydata1, 20))
structure(list(`Time [hr]` = c(0, 0.26, 0.63, 0.96, 1.31, 1.63, 
1.98, 2.3, 2.63, 2.96, 3.38, 3.78, 4.16, 4.56, 4.95, 5.28, 5.65, 
0, 0.26, 0.63), Metabolite = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("G6P", 
"FBP", "Malate", "Citrate"), class = "factor"), `Relative Flux (%)` = c(4.720286, 
0.1610225, 0.1215011, 0.07619009, 0.02090246, 0.01812352, 0.02423107, 
0.05935092, 0.08659023, 0.1906476, 0.2139404, 0.4618791, 0.2807972, 
0.4641912, 0.6142707, 0.8049576, 0.7756669, 157.3645, 4.495637, 
0.9071313)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

Thanks in advance for the help

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Which data element do you want to map to the color aesthetic? You've mapped Metabolite to shape, but nothing to color... – cory Nov 11 '20 at 13:20
  • Can you post sample data? Please edit **the question** with the output of `dput(Mydata1)`. Or, if it is too big with the output of `dput(head(Mydata1, 20))`. – Rui Barradas Nov 11 '20 at 16:28
  • I'm trying to map the color to Metabolite – Oli Newton Nov 11 '20 at 22:35

2 Answers2

0

You need to indicate a color aesthetic in the geom_point call. I used Metabolite but you can use any relevant column there.

geom_point(aes(color=Metabolite), size=4)
Jeffrey Brabec
  • 481
  • 6
  • 11
0

Without a reproducible example, it is harder for us to provide solutions that reflect your particular data.

So here is a representation using the airquality dataset from the datasets package. You can accomplish this by mapping the color within aes for the geom_point.

library(dplyr)
library(ggplot2)

datasets::airquality %>% 
  group_by(Month) %>% 
  summarise(avg_temp = mean(Temp)) %>% 
  ggplot(aes(x = Month, y = avg_temp)) +
  geom_line() +
  geom_point(aes(color = avg_temp))

If you don't want to display the legend, you can add the following:

  geom_point(aes(color = avg_temp)) +
  theme(
    legend.position = "none"
  )

Created on 2020-11-11 by the reprex package (v0.3.0)

Eric
  • 2,699
  • 5
  • 17