I have the below data frame to demonstrate what I want.
library(ggplot2)
df1 <- data.frame(Methods = c('b', 'a', 'c', 'd', 'e'), lb = c(9, 7, 9, 4, 9), RMSE = c(0.26177952, 0.11294586, 0.02452239, 0.08290467, 0.41488542))
df1
Methods lb RMSE
1 b 9 0.26177952
2 a 7 0.11294586
3 c 9 0.02452239
4 d 4 0.08290467
5 e 9 0.41488542
An answer make the below of the R code above:
The lollipop plot simply reveals one thing which is the height of RMSE
.
What I Want
In addition to the height of the
RMSE
, I want the colour to show the distribution of thelb
column.In the case of item 1 above, the
lb
variable (9
,7
,9
,4
,9
) and values oflb
should be attached to the chart legend.I want the minimum value of the
lb
variable to carrygreen
the next minimum to carryyellowgreen
, the middle value to carryyellow, the penultimate maximum to carry
orangeand the maximum value of the
lbvariable to carry
red`.If two methods have the same value of
lb
, give them the same colour then continue to the next value oflb
with the next colour.
The above is what I have summarized in the :
CodePudding user response:
I am not sure if this is what you want, but you can use the following code:
library(tidyverse)
df1 %>%
mutate(lb = as.factor(lb)) %>%
ggplot(aes(x = Methods, y = RMSE, colour = lb))
geom_point(size = 4)
geom_segment(aes(x = Methods, xend = Methods, yend = RMSE, y = 0))
scale_color_manual(values = c("green", "yellow", "red"))
theme_bw()
Output:
CodePudding user response:
along these lines?
df1 |>
ggplot(aes(x = Methods,
xend = Methods,
y = RMSE,
yend = 0,
col = cut(lb, 5, labels = c('XS','S','M','L','XL'))
)
)
geom_point(pch = 20, size = 4)
geom_segment()
scale_color_manual(values = c(XS = 'green', S = 'yellowgreen',
M = 'yellow', L = 'orange', XL = 'red'))
edit: in case of tied lb levels, you could add a rank
ed lb and use it for colouring like so:
df1 |>
mutate(lb_rank = rank(lb)) |>
ggplot(aes(x = Methods,
xend = Methods,
y = RMSE,
yend = 0,
col = cut(lb_rank, 5,
labels = c('XS','S','M','L','XL'))
)
) ## ... remaining stuff