Home > Software design >  I cannot visualize total data well on ggplot2 histogram in R language
I cannot visualize total data well on ggplot2 histogram in R language

Time:05-28

I'm an amateur in R language. I have a total data of something in Dataframe.

data <- data.frame(coun =country, tot = total, yr=year)
data <- data[ with(data, order(tot,decreasing = TRUE)),]
rownames(data) <- NULL
head(data)

Output of this chunk.

There is no problem for now. But:

ggplot(data = data, aes(x=tot))  geom_histogram()

When I typed something to visualize the data, I came across a graph like this:

Output of above code.

The data from the tot column will be visualized in this graph, which will contain nation names on the x-axis.

What should I do, guys?

Thanks in advance!

CodePudding user response:

A histogram is used to visualise the distribution of a continuous variable, but you're trying to plot count data stratified by a categorical variable - for this, you use a bar chart.

Because your data is already in summary form, you use geom_bar():

library(ggplot2)

data <- data.frame(coun = c("UNITED STATES", "CHINA (MAINLAND)", "USSR", "JAPAN", "SINGAPORE", "RUSSIAN FEDERATION"),
           tot = c(3139676, 2950105, 1144547, 876384, 742819, 691644))

ggplot(data, aes(coun, tot))   geom_col()

Created on 2022-05-27 by the reprex package (v2.0.1)

PS: please don't post your data as screenshots - I had to manually recreate your dataset. Read here so that your next questions are more likely to get help.

  •  Tags:  
  • r
  • Related