-1

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!

  • You should ask a specific question that explains what you want to do. Are you trying to count the number of values of `tot`? Or to plot `tot` for each country? Or what? – dash2 May 27 '22 at 14:42
  • Thanks for pointing this out. Post has been edited thanks to you. I want to visualize the data of tot column and this graph will include country names on the x-axis. – Alican Akca May 27 '22 at 14:51
  • You seem to be describing a bar chart, not a histogram. Maybe `ggplot(data = data, aes(x=count, y=tot))+ geom_col()` is more what you want. – MrFlick May 27 '22 at 15:22

1 Answers1

1

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.

Andrea M
  • 2,314
  • 1
  • 9
  • 27