Geom_Col Is Assigning the Wrong Independent Variable

geom_col is assigning the wrong independent variable

You may force the orientation in geom_col() with the "orientation" argument.

From ?geom_col:

orientation The orientation of the layer. The default (NA) automatically determines the orientation from the aesthetic mapping. In the rare event that this fails it can be given explicitly by setting orientation to either "x" or "y".

[geom_col] treats each axis differently and, thus, can thus have two orientations. Often the orientation is easy to deduce from a combination of the given mappings and the types of positional scales in use. Thus, ggplot2 will by default try to guess which orientation the layer should have. Under rare circumstances, the orientation is ambiguous and guessing may fail. In that case the orientation can be specified directly using the orientation parameter, which can be either "x" or "y". The value gives the axis that the geom should run along, "x" being the default orientation you would expect for the geom.

See also discussion in the issue Unexpected (?) orientation with geom_col.

ggplot (DF, aes(x = Depth, y = Rb)) +
geom_col(orientation = "x")

Sample Image

Labels on wrong dodged columns in geom_col

The problem of switching labels comes from geom_text() not knowing how the information should be split for the purposes of dodging. The solution is to supply a group= aesthetic to geom_text() that matches the fill= aesthetic specified for geom_col().

In the case of geom_col(), you specify aes(fill=feature). The height of the different columns is therefore grouped automatically by corr$feature. You can supply a group= aesthetic as well, but it's unnecessary and the dodging will happen as you expect.

In the case of geom_text(), there is no obvious way to group the data. When you do not specify a group= aesthetic, ggplot2 chooses one of the columns (in this case, the first column number) for grouping. For dodging to work here, you need to specify how the label information is grouped. If you don't have a specific legend-associated aesthetic to choose here, you can use the group= aesthetic to specify group=feature. This let's ggplot2 know that the text labels should be sorted and dodged by grouping according to this column in the data:

ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, group=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')

Sample Image

As a side note, you don't have to specify the group= aesthetic if you assign a color-based aesthetic (or one that would result in a legend). If we set color=feature with geom_text(), it works without group=. To see the labels, you need to set the alpha for the columns a bit lower, but this should illustrate the point well:

ggplot(data=corr, aes(x=factor(dimension), y=score)) + 
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single'), alpha=0.2) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, color=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5)

Sample Image

Why do geom_bar bars come from the y-axis and not the x-axis?

I think you're looking for orientation = "x":

ggplot(data, aes(x=bp, y=peak)) +
geom_point() +
geom_bar(stat = "identity", orientation = "x")

Sample Image

Incidentally, geom_bar(stat = "identity") is just a long way of writing geom_col()

Calculating percentages within category using geom_col

To achieve your desired result filter your data for attend == 1 values after computing the percentages.

Note: The blacks lines appear because of overplotting, i.e. as you set position = "dodge" the bars for attend=0 and attend=1 are plotted on top of each other.

Using some random example data:

library(tidyr)
library(dplyr)
library(ggplot2)

set.seed(123)

d <- data.frame(
race = sample(c("Asian", "White", "Hispanic", "Black", "Other"), 100, replace = TRUE),
attend = sample(0:1, 100, replace = TRUE)
)

d %>%
drop_na(attend) %>%
count(race, attend) %>%
group_by(race) %>%
mutate(percent = n/sum(n)*100) %>%
filter(attend == 1) %>%
ggplot(aes(reorder(race, percent), percent, fill = race)) +
geom_col()

Sample Image

Color/fill bars in geom_col based on another variable?

Do the following

  • add fill = sep_mean to aes()
  • add + scale_fill_gradient()
  • remove mutate(colors = brewer.pal(n = 3, name = "PuBu")) since the previous step takes care of colors for you
set.seed(124)

iris[sample(1:150, 50), ] %>%
group_by(Species) %>%
summarise(n=n(), sep_mean = mean(Sepal.Width)) %>%
arrange(desc(n)) %>%
mutate(Species=factor(Species, levels=Species)) %>%
ggplot(aes(Species, n, fill = sep_mean, label=sprintf("%.2f", sep_mean))) +
geom_col() +
scale_fill_gradient() +
labs(fill="Sepal Width\n(mean cm)") +
geom_text()

Sample Image



Related Topics



Leave a reply



Submit