-1

RplotThe values labels are occupying the same space in a way we can't read the graphic. How can I solve this? The only way I can think is to change the variable levels name itself, but the dataset is huge and I don't know how to do it either. Is there a way to change the labels in the axis?

ggplot(data = brfss2013, aes(x = X_smoker3, y = educa), bins = 10) + labs(x = "Computed Smoking Status", y = "Education level") + geom_bar(stat='identity', col = "darkorange", fill = "darkorange")
  • Can you provide a reproducible example of your data? It will help people answer your question. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Jun 19 '20 at 00:22
  • Hello, I'm so sorry! I didn't know how can I do this, this is my very first question here. This dataset is very huge with almost 500k observations and more than 300 variables. I'ill figure this out and post here – Eduardo Schütz Jun 19 '20 at 04:37

2 Answers2

1

I don't know the data details, but it seems to me both X_smoker3 and educa are categorical variables, and you plot is trying to plot the count in each subgroups defined by different levels from those two categorical variables.

I would suggest you use colors to represent different levels of one of the categorical variables (say educa here). Then you plot will have X_smoker3 as y_axis, count as x_axis, and different color represents different level of educa.

I don't have your data, but I use dataset diamonds from ggplot2 package as an example to show the solution. Both clarity and color here are categorical variables, after I flip the coordinates, levels of clarity will be my y_axis, count is x_axis, levels of color is represented as different colors.

ggplot(data = diamonds, mapping = aes(x = clarity,fill = color))+
  layer(geom = "bar",stat = "count",position = "identity")+
  coord_flip()

the generated plot is here

Xiang
  • 314
  • 1
  • 9
  • Hello, I'm so sorry! I didn't know how can I do this, this is my very first question here. This dataset is very huge with almost 500k observations and more than 300 variables. I'ill figure this out and post here. And thank you very much for the help! I will try to reproduce what you said and I will come back – Eduardo Schütz Jun 19 '20 at 04:38
0

We can't reproduce your problem, because you haven't provided a dataset, but in ggplot version 3.3.0 or above, you can dodge the labels. For example you could add:

ggplot(data = diamonds, mapping = aes(x = clarity,fill = color))+
  layer(geom = "bar",stat = "count",position = "identity")+
  coord_flip() + scale_x_discrete(guide = guide_axis(n.dodge=3))

See: https://datavizpyr.com/how-to-dodge-overlapping-text-on-x-axis-labels-in-ggplot2/

william3031
  • 1,653
  • 1
  • 18
  • 39
  • Hello, I'm so sorry! I didn't know how can I do this, this is my very first question here. This dataset is very huge with almost 500k observations and more than 300 variables. I'ill figure this out and post here. And thank you very much for the help! I will try to reproduce what you said and I will come bac – Eduardo Schütz Jun 19 '20 at 04:39