Home > database >  ggplot bar graph - changing colour for value of y
ggplot bar graph - changing colour for value of y

Time:10-26

im new to stackoverflow and need some help please. I wanted to colour y (Value) data above a level of 1.35 and cant seem to get it to work. wanted Steelblue for below 1.35 and plum for above 1.35. Below is working graph minus the colour change.

dataframe i have is

Timestamp  Value 
20/10/2021 1.56
21/10/2021 1.426
22/10/2021 1.344
23/10/2022 1.23
24/10/2022 1.11

water$Timestamp <- water$Timestamp %>% dmy
river <- ggplot()   
  coord_cartesian()  
  layer(
    data = water,
    mapping = aes(x = Timestamp, y = Value),
    stat = "identity",
    geom ="point", 
    position = position_identity()
  )  
  layer(
    data = riverwater,
    mapping = aes(x = Timestamp, y = Value),
    stat ="identity",
    geom ="line",
    position = position_identity()
  )
ggplotly(river)

CodePudding user response:

Your {ggplot2} syntax is a bit unusual, but to get the colors assigned by the Value you need to assign color inside aes().

library(tidyverse)
library(lubridate)
library(plotly)

d <- tibble(Timestamp = c("20/10/2021", "21/10/2021", "22/10/2021", "23/10/2022", "24/10/2022"), 
            Value = c(1.56, 1.426, 1.344, 1.23, 1.11))

p <- d %>% 
  mutate(Timestamp = dmy(Timestamp)) %>% 
  ggplot(aes(x = Timestamp, y = Value, color = Value > 1.35))  
  geom_point()  
  geom_line()  
  scale_color_manual(values=c("Steel blue","plum"))

p

# ggplotly(p)

Created on 2022-10-25 with reprex v2.0.2

CodePudding user response:

[this is the example of non continuous graph of the 2 plots, one line and other point enter image description here

  • Related