Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lollypop plot with a Customizes Colour in R

I have a data frame that consists of three (3) variables (Methods, lb and RMSE).

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

I want the Methods(a, b, c, d, e) to be arranged on the x-axis, the height of the lollypop plot to correspond to the values of RMSE while the colours on the lillyplot to vary among the lb variable.

I have tried this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df1 |>
  ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE)) + ggplot2::geom_point(size = 4) + 
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) + 
ggplot2::scale_fill_manual(values = c("green", "yellowgreen", "yellow", "orange", "red"))

I got this Image

df1 |>
  ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE)) + ggplot2::geom_point(size = 4) + 
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) + 
ggplot2::scale_fill_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) + 
ggplot2::theme_bw() + ggplot2::scale_colour_gradientn(colours = rev(rainbow(5)))

When I changed from manual the image is still the same without colour.

What I want

I want the pop with colours such that I can customize the pop’s colours

>Solution :

First of all, you don’t really need to do ggplot:: in all functions. It’s just a syntax to make sure the functions you use are from the specific package package:: you want. For ggplot functions, I’m pretty sure there’s no conflicting function names.

Second, if you want scale_fill_manual to work, you need to use aes(fill = var) in geom_function() or ggplot().

  • If you want the fill or colour to apply to all geom_*() layers, use ggplot(aes(fill = var)).
  • Otherwise, only use it in the desired geom_*() layer, which is in this case, in geom_point(aes(color = var)) (Note that in my code, I placed it in ggplot, so geom_segment is also coloured accordingly).

Third, since you’re colouring geom_point, you should use scale_color_manual instead of scale_fill_manual (and also aes(color = var).

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 |>
  ggplot(aes(x = Methods, y = RMSE, col = Methods)) + geom_point(size = 4) + 
  geom_segment(aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) + 
  scale_color_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) + 
  theme_bw()

Created on 2022-04-14 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading