Search code examples
rggplot2geom-bar

How to change color (automatically) in geom_line() using R programming language


I have generated a graph using ggplot2 and geom_line(). My graph contains more than 'one' line but all lines are showing only one color (black) but I need different colors for a different line.

My code is given below:

ggplot(data=data_1, aes(x=Month, y=rain, fill=factor(Year)))+
  geom_line(stat="identity")+
  theme_minimal()+
  #geom_col(width = 0.05, position = position_dodge(0.7))+
  xlim(0,12)+
  ylim(0,800)+
  xlab("Month")+
  ylab("Rain")+
  ggtitle("Rain according to Month") +
  guides(fill = guide_legend(ncol = 2))

Code generated graph

but I am trying to do something like this graph.

Expected Graph

A snapshot of my data is given below:

    tem      Month Year   rain
1   16.9760     1 1901  18.53560
2   19.9026     2 1901  16.25480
3   24.3158     3 1901  70.79810
4   28.1834     4 1901  66.16160
5   27.8892     5 1901 267.21500
6   28.8925     6 1901 341.04200
7   28.3327     7 1901 540.90700
8   27.9243     8 1901 493.21000
9   27.6057     9 1901 291.54900
10  27.0887    10 1901 199.17100

I also tried to fix the x-axis limit using xlim but it is also not working?

Is it also possible to show the data (output) when I will hover my cursor on the graph point?

Could you tell me what I have to do?


Solution

  • Try first creating the plot with ggplot2 functions and be careful that fill and color are different things. In order to have a dynamic style you could use ggplotly() from plotly package that allows for some interactive values and customization. Here the code:

    library(plotly)
    #Code
    G1 <- ggplot(data=data_1, aes(x=Month, y=rain, color=factor(Year)))+
      geom_line(stat="identity")+
      theme_minimal()+
      #geom_col(width = 0.05, position = position_dodge(0.7))+
      xlim(0,12)+
      ylim(0,800)+
      xlab("Month")+
      ylab("Rain")+
      ggtitle("Rain according to Month") +
      guides(fill = guide_legend(ncol = 2))
    #Dynamic
    ggplotly(G1)
    

    Output:

    enter image description here

    Some data used:

    #Data
    data_1 <- structure(list(tem = c(16.976, 19.9026, 24.3158, 28.1834, 27.8892, 
    28.8925, 28.3327, 27.9243, 27.6057, 27.0887, 16.976, 19.9026, 
    24.3158, 28.1834, 27.8892, 28.8925, 28.3327, 27.9243, 27.6057, 
    27.0887, 16.976, 19.9026, 24.3158, 28.1834, 27.8892, 28.8925, 
    28.3327, 27.9243, 27.6057, 27.0887), Month = c(1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Year = c(1901, 
    1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1902, 1902, 
    1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 
    1902, 1902, 1902, 1902, 1902, 1902, 1902), rain = c(18.5356, 
    16.2548, 70.7981, 66.1616, 267.215, 341.042, 540.907, 493.21, 
    291.549, 199.171, 18.5356, 16.2548, 70.7981, 66.1616, 267.215, 
    341.042, 540.907, 493.21, 291.549, 199.171, 118.5356, 116.2548, 
    170.7981, 166.1616, 367.215, 441.042, 640.907, 593.21, 391.549, 
    299.171)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", 
    "9", "10", "11", "21", "31", "41", "51", "61", "71", "81", "91", 
    "101", "12", "22", "32", "42", "52", "62", "72", "82", "92", 
    "102"), class = "data.frame")