How to plot a barplot using ggplot2

thole

I have a dataframe, that I want to use to plot a bar plot. The numeric variable is jumbled and I sorted it into descedning order. I want to plot the new sorted dataframe using ggplot but the barplot is not giving me the desired output.

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.frame(Name, Age)

print (df)
  Name Age
1   Jon  23
2  Bill  41
3 Maria  32
4   Ben  58
5  Tina  26

#sort in descending order
df2<-df%>%
  arrange(desc(Age))
print(df2)

The outcome is below:
 Name Age
1   Ben  58
2  Bill  41
3 Maria  32
4  Tina  26
5   Jon  23

To plot it my attempt is below. But now it does not plot them in the descending manner I was expecting.

#plot it using gglot
df2%>%ggplot(aes(x=Name, y=Age)) +
  geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
  coord_flip() +
  xlab("") +
  theme_bw()

this the output it gives me: enter image description here

Andy Baxter

To plot a character vector in an order other than alphabetical you turn it into a factor. Either factoring after ordering or using the forcats:fct_reorder function to do both at the same time would work:

library(ggplot2)

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.frame(Name, Age)

# Reorder using -Age (to order descending)

df |> ggplot(aes(x=forcats::fct_reorder(Name, -Age), y=Age)) +
  geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
  coord_flip() +
  xlab("") +
  theme_bw()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to plot a barplot using ggplot

How to plot multiple time series with a reverse barplot on the secondary axis using ggplot2?

How to change colour for barplot in ggplot2 using viridis package?

how to plot scatter plot in hexagon using R or ggplot2?

How to plot a (sophisticated) stacked barplot in ggplot2, without complicated manual data aggregation

ggplot2: How to plot barplot, where bars signify percentage and are colored according to percentage value?

How to plot plots using different datasets using ggplot2

ggplot2 barplot labels using counts

Multiple barplot using ggplot2

Barplot using ggplot2 for 4 variables

How to generate weighted mean plot using ggplot2?

How to create a double bar contrast plot using ggplot2

How to plot multiple different state scatterplots using ggplot2?

how to plot a histogram using ggplot2 with a slightly different data

How to get axis scale and ticks in this plot using ggplot2?

How to add summary statistics in histogram plot using ggplot2?

How to recreate following Box and Whisker Plot using ggplot2?

How to do a fancy box-plot using ggplot2?

How to put label on bar plot using ggplot2?

How can i plot an fda object using ggplot2?

How to plot a triangle heatmap using ggplot2?

Plot using ggplot2

Building a binary sparkline plot in R with ggplot2 barplot

How do I create a non-stacked barplot with data labels using ggplot2 in R?

How to create a stacked barplot using Groups to determine Fill and X variable to determine Color (ggplot2)?

How to plot a zoom of the plot inside the same plot area using ggplot2?

How create a box plot + line plot in a single plot using ggplot2

How to plot a empty barplot?

How to plot the following barplot?