Ordering factors in number order for ggplot
You can use reorder()
:
ggplot(df, aes(x = reorder(x, sort(as.numeric(x))), y = y)) +
geom_bar(stat = "identity")
Ordering Several factor variables in -axis ggplot
As you noted, the order on the x-axis is in alphabetic order, and not numerical order. Here is one approach to fix that.
Using the str_sort
function from the stringr
package you can take:
vec <- c("sb_1_x", "sb_10_b", "sb_11_c", "sb_2_y")
vec
[1] "sb_1_x" "sb_10_b" "sb_11_c" "sb_2_y"
and order by the middle number:
str_sort(vec, numeric = T)
[1] "sb_1_x" "sb_2_y" "sb_10_b" "sb_11_c"
In this case, make sure sb
is a factor, and use str_sort
to set the factor's levels. I also renamed the x-axis label (you can replace with what you need). Putting it all together:
library(tidyverse)
library(ggplot2)
library(stringr)
df %>%
pivot_longer(cols = starts_with("sb")) %>%
filter(value != 0) %>%
unite(sb, name, value) %>%
ggplot(aes(x = factor(sb, levels = str_sort(unique(sb), numeric = TRUE)))) +
geom_bar(aes(fill = wave), position = position_dodge2(preserve = "single")) +
xlab("x-axis label")
Plot
How can I reorder factors by proportion in ggplot efficiently?
You can use fct_reorder
from the forcats
package. This package was mentioned several times in the first question you linked as well:
# data
dframe <- data.frame(
variable = rep(c("alpha", "beta", "gamma", "delta"), each = 4),
class = c(rep("1", 4),
"1", "1", "0", "0",
rep("0", 3), "1",
rep("1", 3), "0"))
dframe %>%
# convert variable to a factor, ordered (in descending order) by the proportion of
# rows where the class == "1"
mutate(variable = forcats::fct_reorder(.f = variable,
.x = class,
.fun = function(.x) mean(.x == "1"),
.desc = TRUE)) %>%
ggplot(aes(x = variable, fill = class)) +
geom_bar(position = "fill") +
labs(y = "Proportion")
ggplot - prevent ordering of character or factor x-axis values
You should create an ordered factor to prevent the order. For example
mydata$x <- factor(maydata$x,
levels = c('1 Dog', '2 Dog', '3 Dog', '4 Dog', '1 Frog', '2 Frog', '1 Cat', '2 Cat'),
ordered = T)
In the levels argument you should mention the levels in the specific order you want to see in the plot. The argument ordered = T
preserves the order.
Ordering ggplot2 legend to agree with factor order of bars in geom_col when plotting data from tabyl
So it turns out the problem was this bit in the geom_col
portion of the ggplot
code: fill = str_wrap(Category,40)
. Somehow that fill argument didn't play well with scale_fill_discrete
, which is why Jared's initial solution didn't work, but his updated answer gets us most of the way there.
So the solution steps were:
- Remove the
str_wrap
command from thegeom_col
fill
argument. - Add
scale_fill_discrete(labels = ~ stringr::str_wrap(.x, width = 40))
to the end of theggplot
code. - Add
y = "Category"
to the labs element in the ggplot (to override the yucky y axis title that would otherwise result from the reordering command).
Huge thanks to @jared_mamrot for helping me troubleshoot!
Also appropriate citation from another post that offered the solution: How to wrap legend text in ggplot?
library(tidyverse)
library(ggplot2)
library(forcats)
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
temp <- tribble(
~ Category,
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"CCCCC CCC CCC CC CCCCC CCC CCCCCCCCCC CCCC CCCCC CCCCCCCCC CCCCCCCCCCC CCCC CCC CCC C CCC",
"CCCCC CCC CCC CC CCCCC CCC CCCCCCCCCC CCCC CCCCC CCCCCCCCC CCCCCCCCCCC CCCC CCC CCC C CCC",
"CCCCC CCC CCC CC CCCCC CCC CCCCCCCCCC CCCC CCCCC CCCCCCCCC CCCCCCCCCCC CCCC CCC CCC C CCC",
"CCCCC CCC CCC CC CCCCC CCC CCCCCCCCCC CCCC CCCCC CCCCCCCCC CCCCCCCCCCC CCCC CCC CCC C CCC",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"EEEE",
"EEEE",
"EEEE",
"EEEE",
)
temp_n <- temp %>%
nrow()
temp_tabyl <-
temp %>%
tabyl(Category) %>%
mutate(Category = factor(Category,levels = c("DDDDD DD D DDD DDDD DDD DDDDDDD DDD DDDD DDDDDDD DDD DDD DDDD DDDDDDDDD DDDD DDDDD DDDDDDD",
"BBBB BBBB BBBBB B BBBBBB BBBBB BBBBBB BBBBB BBBBB B BBBB BBBBB BBBBBBB",
"AAAAAAA AAAAAAAA AAAAAAAAA AAAAAAAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAAA AAAAAAAAAA",
"CCCCC CCC CCC CC CCCCC CCC CCCCCCCCCC CCCC CCCCC CCCCCCCCC CCCCCCCCCCC CCCC CCC CCC C CCC",
"EEEE"))) %>%
rename(Percent = percent) %>%
arrange(desc(Percent)) %>%
mutate(CI = sqrt(Percent*(1-Percent)/temp_n),
MOE = CI * 1.96,
ub = Percent + MOE,
lb = Percent - MOE)
temp_tabyl %>%
ggplot() +
geom_col(aes(y = reorder(Category,Percent),
x = Percent,
fill = Category),
colour = "black"
) +
geom_errorbar(
aes(
y = reorder(Category,Percent),
xmin = lb,
xmax = ub
),
width = 0.4,
colour = "orange",
alpha = 0.9,
size = 1.3
) +
labs(colour="Category",
y = "Category") +
geom_label(aes(y = Category,
x = Percent,
label = scales::percent(Percent)),nudge_x = .11) +
scale_x_continuous(labels = scales::percent,limits = c(0,1)) +
labs(title = "Plot Title",
caption = "Plot Caption.") +
theme_bw() +
theme(
text = element_text(family = 'Roboto'),
strip.text.x = element_text(size = 14,
face = 'bold'),
panel.grid.minor = element_blank(),
axis.title.y = element_text(size = 14),
plot.title = element_text(hjust = 0.5, size = 16),
plot.subtitle = element_text(hjust = 1),
plot.caption = element_text(hjust = 0),
axis.text.y=element_blank()
) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(strip.text = element_text(colour = 'white'),
legend.spacing.y = unit(.5, 'cm')) +
guides(fill = guide_legend(as.factor('Category'),
byrow = TRUE)) +
scale_fill_discrete(labels = ~ stringr::str_wrap(.x, width = 40))
Created on 2022-06-20 by the reprex package (v2.0.1)
Plot values with a certain order based on another column in ggplot
If you want to make the factor appears as in the data, refactor the variable with levels as unique(your_column)
, which will use the levels as they appear in the dataset.
Your points are ordered in the reversed order, just use scale_x_discrete(limits=rev)
to make it appear the correct way:
mydata %>%
mutate(Taxa = factor(Taxa, levels = unique(Taxa))) %>%
ggplot(aes(x = Taxa, y = Number)) +
geom_segment(aes(x = Taxa, xend = Taxa, y = 0, yend = Number), color = "grey") +
geom_point(size = 3, color = "darkorange") +
coord_flip() +
scale_x_discrete(limits=rev)
More thorough explanation:
Factor is a class and contains levels. ggplot
orders the factor variable in the plot according to the order of the levels (so not according to the order as they appear in the data). If you order the dataset, the order of the levels will remain the same, i.e., you have to explicitly reorder the levels to do that. So one way to reorder the levels is to use factor
and explicitly set the levels
in the order of the data using unique
. It may be a bit cumbersome, but that's the cleanest way to deal with factors.
Ordering factors in each facet of ggplot by y-axis value
I've found dplyr doesn't work super well with group_by()
when dealing with different factor levels in each of the groups. So one work around is thinking of creating a new factor that's unique for each animal-letter combination and ordering that. First, we create an interaction variable with animal+letter and determine the proper order for each of the letters for the animals
new_order <- my_df %>%
group_by(animals) %>%
do(data_frame(al=levels(reorder(interaction(.$animals, .$letters, drop=TRUE), .$numbers)))) %>%
pull(al)
Now we create the interaction variable in the data we want to plot, use this new ordering, and finally change the labels so they look like just the letters again
my_df %>%
mutate(al=factor(interaction(animals, letters), levels=new_order)) %>%
ggplot(aes(x = al, y = numbers)) +
geom_point() + facet_wrap(~animals, ncol = 1, scales = 'free_x') +
scale_x_discrete(breaks= new_order, labels=gsub("^.*\\.", "", new_order))
Related Topics
Fitting a Lognormal Distribution to Truncated Data in R
Na Matches Na, But Is Not Equal to Na. Why
Unpacking and Merging Lists in a Column in Data.Frame
Rmarkdown::Render() in a Loop - Cannot Allocate Vector of Size
How to Tell Which Packages I am Not Using in My R Script
How to Use "Cast" in Reshape Without Aggregation
Shiny App Does Not Reflect Changes in Update Rdata File
How to Calculate the Median on Grouped Dataset
How to Change Factor Labels into String in a Data Frame
R Shiny Action Button and Data Table Output
Alpha Aesthetic Shows Arrow's Skeleton Instead of Plain Shape - How to Prevent It
Sort Boxplot by Mean (And Not Median) in R
How to Correctly 'Dput' a Fitted Linear Model (By 'Lm') to an Ascii File and Recreate It Later
How to Use a Non-Ascii Symbol (E.G. £) in an R Package Function