-1

I have a big dataset, and I have made a plot of a histogram, but the categorical variables "type of crime" on the x-axis is not visible. How do I make it visible in r?

g <- ggplot(df, aes(x = TYPE.OF.CRIME))
g + geom_histogram(bins = 30, color = "gray", stat = "count")  

Warning message: Ignoring unknown parameters: binwidth, bins, pad

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • 1
    Please share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Martin Gal Jun 26 '20 at 10:12

2 Answers2

2

It's difficult to know exactly what you're looking for. Perhaps a bar chart rather than a histogram?

library(ggplot2)

g <- ggplot(df, aes(x = TYPE.OF.CRIME, fill = TYPE.OF.CRIME)) 
g + geom_bar()

enter image description here


Data

df <- data.frame(TYPE.OF.CRIME = sample(c("No reproducible data included",
                                  "Not stating problem clearly",
                                  "Not indicating packages used"), 30, replace = TRUE),
           stringsAsFactors = TRUE)
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

geom_histogram() probably should not have categorical values without levels on its x axis as it works with intervals, you probably want geom_bar() rather than histogram?

g <- ggplot(df, aes(x = TYPE.OF.CRIME))
g + geom_bar(color = "gray")
  • i actualy want a histogram, from the bar graph you plot, where there is "no reproducible data" , "not tating problem clearly",..., I want type of crime e.g "robery","assult" and ect.. all the type of crime should appear on the x-axis just like you have done in the bargraph above. – augustinus ntjamba Jun 26 '20 at 13:08
  • @augustinusntjamba That's actually not my plot, that's answer of @AllanCameron, my answer is only this short one! Just use Allan's code, only `df` states for your own data frame. –  Jun 26 '20 at 13:12
  • @augustinusntjamba It works, but it doesn't make sense as you have onordered categorical data like types of crime. The graphical output of geom_bar() is probably what you want - you get bars with counts of all the categories. –  Jun 26 '20 at 13:23
  • i have just post a question on the platform with a graph, perhaps if you can have a look at it , and see what my question is point at. – augustinus ntjamba Jun 26 '20 at 13:37