简体   繁体   中英

alpha and dodge for geom_bar in ggplot2

Good afternoon.

I am having a problem trying to plot a barplot using ggplot2 : I would like to plot 3 variables of different colors on the same histogram. For that I use geom_bar with a specific level of grey attributed to each variable.I am using position=position_dodge(width=0.75) to overlay the bars and using alpha=0.85 to create transparency. However, I can't figure out why the last variable appears to be on top of the second one.I would like the first variable covering the second variable (as it is) and the second one covering the third one (which is the problem here). What does control the "depth" of each variable and is there a way to change this? Histogram presenting my 3 variables, as you can see, the black variable appears on top of the dark grey one (which is great) but the light grey variable also appears on top of the dark grey variable (instead of under it)

Many thanks.

To the best of my knowledge the issue comes from the position=position_dodge .

Here you specify the distance between centers is lower than the width of your bars. You should try with position="dodge" instead.

Although I still don't know the correct answer to your question, I was interested in this topic and tested some of my data varying colors and width.

In my cases, the third bar covered the second, and the second bar covered the first one, always. A tricky part is that it is very hard to tell which one covers which without adding line colors (or decreasing width). I guess there might be a chance that actually your third bar covers the second, and the second bar covers the first one like my cases.

Please try to add colour="black" in your geom_bar() function, and see if the overlaying order is the same as you thought.

在此输入图像描述

在此输入图像描述

在此输入图像描述 在此输入图像描述

Alright, For those struggling with the same problem, I eventually found the way this works. The order of the columns (horizontally) is determined alphabetically by the name of the three variables (in my case, "F" is the first bar, "N" is the second bar and "XFe" is the last bar) but the depth of each variable is determine by they order of appearance in the dataset with the first variable appearing being the bar the most at the back and the last appearing variable being the bar the most in front (see attached pictures for example, I put the same colour to the 3 variable to make it more obvious). In my original version, the order of appearance of my variables in the dataset was "N", "F" and "XFe" and here is what's happening when I change the order of the dataset:

Version with dataset reorganised for having the order of appearance of the variables as "F", "N" and "XFe"

Version with appearance of the variables in the dataset as "F", "XFe" and "N"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM