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

How can I control the error bar colors to be the same as geom_point colors?

I wanted to make a point plot in ggplot and I used geom_point function to do it. To colorize the points, I used levels to fill them either in red or blue. However, now I’m adding the error bars to my point plot with geom_errorbar function, it seems I can’t give the colors I used in geom_points. In other words, I can only give them one color, which is by default black. Is there any way I can control the color of the error bars?

Let’s say we have the following data frame:

df <- data.frame (Xvalue=c(1.2, 1.3, 2.1, 2.4, 2.7),
                 Yvalue=c(12, 15, 17, 24, 27),
                 Kind=c('A', 'B', 'A', 'B', 'B'),
                 Ymax=c(13, 16, 19, 26, 29),
                 Ymin=c(11, 14, 15, 22, 25))
df$Kind = factor(df$Kind)

Now I want to draw the plot with the error bars:

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

myplot <- ggplot (df, aes(Xvalue, Yvalue, fill=Kind)) + 
  geom_point( shape=21, size= 4, alpha= 0.7)+
  geom_errorbar(aes(ymin=Ymin, ymax=Ymax,  width=0.08))
myplot

enter image description here
This results in a plot with error bars of black. How can I make sure this is the same color as the points?

Bonus question: Is this how you would usually make two colors in your points, I mean by putting the "levels" in the ggplot function as fill? Do you have an alternative way to where one doesn’t need to use Shape=21, where the outer pirameter of the circle is black?

Bonus qeustion 2: Is it generally easier to plot two kinds of points, when they are in 1 dataframe, or is it better to seperate the dataframes? For instance, I can have a smaller dataframe for the Kind A, and another one for dataframe B. Would that make the life easier? Why I’m asking is that, when I tried to plot with more than one levels (for instance once "Kind" and the other one "Original country"), it was quite confusing to plot points in different colors and sizes.

>Solution :

You should add the color aes to your geom_errorbar like this (Please note: you could add show.legend FALSE to your geom_errorbar so there is no line in your elements of your legend):

library(ggplot2)
myplot <- ggplot (df, aes(Xvalue, Yvalue, fill=Kind)) + 
  geom_point( shape=21, size= 4, alpha= 0.7)+
  geom_errorbar(aes(ymin=Ymin, ymax=Ymax, width=0.08, color = Kind), show.legend = FALSE)
myplot

Created on 2023-01-12 with reprex v2.0.2

If you want the error bar behind your point, just swap the commands in order.

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