Avoid ggplot sorting the x-axis while plotting geom_bar()
You need to tell ggplot that you've got an ordered factor already, so it doesn't automatically order it for you.
dat <- read.table(text=
"SC_LTSL_BM 16.8275
SC_STSL_BM 17.3914
proB_FrBC_FL 122.1580
preB_FrD_FL 18.5051
B_Fo_Sp 14.4693
B_GC_Sp 15.4986", header = FALSE, stringsAsFactors = FALSE)
# make V1 an ordered factor
dat$V1 <- factor(dat$V1, levels = dat$V1)
# plot
library(ggplot2)
ggplot(dat,aes(x=V1,y=V2))+geom_bar(stat="identity")
Prevent ggplot2 from automatically ordering x-axis
This could be an option
# Add a column with order, as per your requirement
cons_mergedAll$order = c(1:length(cons_mergedAll$count))
# use the added column to reorder() your data in ggplot
ggplot(cons_mergedAll , aes(reorder(month_yr2, order) ,count, fill=type )) +
geom_bar( stat="identity",position="dodge") +
ylab("Number of Tickets") +
xlab("Month")
bar chart R geom_bar changing legend and x axis
show.legend = FALSE
will remove the legend. And there is a missing +
after the ggtitle
.
library(ggplot2)
library(dplyr)
Evol_praticiens_spé_amb <- tribble(
~Spécialité, ~`Nombre de praticiens en 2021`,
"a", 10,
"b", 20
)
"Spécialité" <- Evol_praticiens_spé_amb[, 1]
"Nombre de praticiens en 2021" <- Evol_praticiens_spé_amb[, 2]
ggplot(Evol_praticiens_spé_amb, aes(
x = Spécialité, y = `Nombre de praticiens en 2021`,
fill = as.factor(Spécialité)
)) +
geom_bar(stat = "identity", position = "dodge", show.legend = FALSE) +
ggtitle("Repartition des praticiens en ambulatoire en 2021") +
theme(
plot.title = element_text(hjust = 0.5, vjust = 1, size = 10),
axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
legend.key = NULL,
axis.text.y = element_text(size = 5)
)
Created on 2022-06-21 by the reprex package (v2.0.1)
ggplot2 geom_bar - how to keep order of data.frame
Posting as answer because comment thread getting long. You have to specify the order by using the factor levels of the variable you map with aes(x=...)
# lock in factor level order
df$derma <- factor(df$derma, levels = df$derma)
# plot
ggplot(data=df, aes(x=derma, y=prevalence)) +
geom_bar(stat="identity") + coord_flip()
Result, same order as in df
:
# or, order by prevalence:
df$derma <- factor(df$derma, levels = df$derma[order(df$prevalence)])
Same plot command gives:
I read in the data like this:
read.table(text=
"SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1", header=F, sep=',')
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names
ggplot (geom_bar) not sorting y-axis according to numeric values
The functions are all working as intended - the reason you don't see the result as expected is because the reorder()
function is specifying the ordering of the pop_data$PA
based on each observation in the set, whereas the bars you are plotting are a result of summary statistics on pop_data
.
The easiest solution is to probably perform the summarizing first, then plot and reorder the summarized dataset. This way, the reordering reflects an ordering of the summarized data, which is what you want.
temp3 <- pop_data %>% filter(`Time` == '2019') %>%
group_by(PA) %>%
summarize(Pop = sum(Pop))
ggplot(data=temp3, aes(x=reorder(PA, Pop),y=Pop)) +
geom_bar(stat='identity') + coord_flip()
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!
Related Topics
Using R to List All Files with a Specified Extension
Comma Separator for Numbers in R
Extracting Unique Numbers from String in R
Define $ Right Parameter with a Variable in R
Function to Calculate R2 (R-Squared) in R
How to Install an R Package from the Source Tarball on Windows
Grouped Barplot in R with Error Bars
How to Convert R Markdown to HTML? I.E., What Does "Knit HTML" Do in Rstudio 0.96
Add Error Bars to Show Standard Deviation on a Plot in R
Count Number of Columns by a Condition (>) for Each Row
Differencebetween Assign() and <<- in R
How to Increase Font Size in a Plot in R
How to Get the Classes of All Columns in a Data Frame
R Ggplot2 Merge with Shapefile and CSV Data to Fill Polygons