ggplot2, Ordering y axis
As you have variable Mut
in your data that determines to which level each observation belongs, you don't need to use geom_bar()
twice with subset. Just add fill=factor(Mut)
inside the aes()
and use df2 with ordered data. Bars will be in correct order and color made automatically.
ggplot(df2,aes(x=factor(name),y=depth,fill=factor(Mut))) +
geom_bar(stat='identity') +
coord_flip() + labs(y='depth',x='species')
The same result can be achieved with original dataframe df
and function reorder()
inside aes()
for x values.
ggplot(df,aes(x=reorder(name,depth),y=depth,fill=factor(Mut))) +
geom_bar(stat='identity') +
coord_flip() + labs(y='depth',x='species')
How do I change the order of the y-axis in ggplot2 R?
If the column in ts
by which you would like to order y
is called release_date
, you can try reorder()
, with decreasing=T
ggplot(ts, aes(x = danceability, y = reorder(album_name, release_date,decreasing=T)) +
geom_density_ridges_gradient(scale = .9) +
theme_light() +
labs(title = "Danceability by Albums",
x = "Danceability",
y = "Album Name")
While no data were provided, we can see this in action here:
set.seed(123)
data = data.frame(y=rep(letters[1:3],100), x=rnorm(300), o=rep(c(1,3,2),100))
gridExtra::grid.arrange(
ggplot(data, aes(x,y)) + geom_density_ridges_gradient(scale=0.9) + ggtitle("Unordered"),
ggplot(data, aes(x,reorder(y,o,decreasing=T))) + geom_density_ridges_gradient(scale=0.9) + ggtitle("Ordered")
)
How do you specifically order ggplot2 x axis instead of alphabetical order?
It is a little difficult to answer your specific question without a full, reproducible example. However something like this should work:
#Turn your 'treatment' column into a character vector
data$Treatment <- as.character(data$Treatment)
#Then turn it back into a factor with the levels in the correct order
data$Treatment <- factor(data$Treatment, levels=unique(data$Treatment))
In this example, the order of the factor will be the same as in the data.csv
file.
If you prefer a different order, you can order them by hand:
data$Treatment <- factor(data$Treatment, levels=c("Y", "X", "Z"))
However this is dangerous if you have a lot of levels: if you get any of them wrong, that will cause problems.
ggplot facet different Y axis order based on value
The functions reorder_within
and scale_*_reordered
from the tidytext package might come in handy.
reorder_within
recodes the values into a factor with strings in the form of "VARIABLE___WITHIN". This factor is ordered by the values in each group of WITHIN.scale_*_reordered
removes the "___WITHIN" suffix when plotting the axis labels.
Add scales = "free_y"
in facet_wrap
to make it work as expected.
Here is an example with generated data:
library(tidyverse)
# Generate data
df <- expand.grid(
year = 2019:2021,
group = paste("Group", toupper(letters[1:8]))
)
set.seed(123)
df$value <- rnorm(nrow(df), mean = 10, sd = 2)
df %>%
mutate(group = tidytext::reorder_within(group, value, within = year)) %>%
ggplot(aes(value, group)) +
geom_point() +
tidytext::scale_y_reordered() +
facet_wrap(vars(year), scales = "free_y")
ggplot - order of y axis is not like the one of the dataset
I found a different approach to solve this problem.
I changed the order of the variables in the dataframe with
df <- df %>%
map_df(rev)
and then used the first function that Jon Spring suggested in the ggplot command
ggplot(data=df, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
geom_line() +
geom_point() +
coord_flip()
Now I got the right order in the plot.
Thanks for the support!
how to make the y-axis value in order when using ggplot2 in R language
you should try this (with tidyverse):
type1_age %>%
mutate(Value = Value %>% as.character %>% as.numeric) %>%
ggplot(aes(x = Year, y = Value, group = Age, color = Age)) +
geom_line()+
ggtitle('The percentage distribution of type1 diabetes patients in different age groups')+
ylab("percentage (%)")
I suspect you Value variable to be a character vector and not a numeric one. The first part of the code should transform it in one.
Tell me if it works!
Related Topics
Is There a Technical Difference Between "=" and "<-"
Shade Region Between Two Lines with Ggplot
Summing Across Rows of a Data.Table for Specific Columns
Ggplot2: Geom_Text() with Facet_Grid()
Error with Ggplot2 Mapping Variable to Y and Using Stat="Bin"
Ggplot Graphing of Proportions of Observations Within Categories
Problems Using Foreach Parallelization
How to Change the Na Color from Gray to White in a Ggplot Choropleth Map
Dynamic Height and Width for Knitr Plots
Why Does Median Trip Up Data.Table (Integer Versus Double)
Dodging Points and Error Bars with Ggplot
How to Do a Regression of a Series of Variables Without Typing Each Variable Name