0

I'm trying to plot three different .csv columns, the 1st and 2nd as lines and the 3rd as points (only two points). I am a begineer in R and I have the following data

  • data

enter image description here

  • code
    df = read.csv("conceptual_methodology.csv")
    
    ggplot(df, aes(x=time, group=1))+
      geom_line(aes(time, var_1, color = "var_1"), size = 1.2)+
      geom_line(aes(time, var_2, color = "var_2"), size = 1.2)+
      geom_point(aes(time, var_3, color = "red"), size = 3)+
      scale_y_continuous(c(0.26, 0.3), seq(0, 0.3, 0.005))+
      xlab("label_x")+
      ylab("label_y")+
      theme_bw()+
      theme(axis.line = element_line(color = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_rect(color = "black"),
            axis.ticks.x=element_blank(),
            axis.text.x=element_blank(),
            panel.background = element_blank())

The outcome is the following image: Rplot

Obviously, my code is not brilliant and I have the following problems with the result:

  1. ylab does not appear
  2. numbers in x axis do not appear
  3. I don't understand why everything appears as line and point in the legend, and not var_1 & var_2 just lines and var_3 just point.
  4. Why y axis stops at 0.295 and not 0.3 as specified in scale_y_continuous.
  5. How can I change the colors of the lines and points without changing the names in the legend. For example, when I put color="red" in geom_point the name of the series becomes red in the legend.

Can someone please fix my code?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
K. Kaf
  • 13
  • 1
  • Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064), share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so it's easier to copy/paste the data for testing. – MrFlick Nov 04 '22 at 14:17
  • 1
    for point 2) remove `axis.text.x=element_blank()` – AndS. Nov 04 '22 at 14:24
  • 1
    for point 1) and 4) add argument names to `scale_y_...`: `scale_y_continuous(name = "label_y", limits = c(0.26, 0.3), breaks = seq(0, 0.3, 0.005))` – AndS. Nov 04 '22 at 14:25
  • 1
    lastly, for point 5) add the color outside of the `aes` – AndS. Nov 04 '22 at 14:27
  • Thanks a lot! I have to struggle a bit with the rest of it, but very helpful advice and it looks much better now. – K. Kaf Nov 06 '22 at 09:10

0 Answers0