neto333
neto333

Reputation: 91

geom_bar overlapping labels

for simplicity lets suppose we have a database like

# A    
1 1  
2 2   
3 2    
4 2
5 3   

We have a categorical variable "A" with 3 possible values (1,2,3). And im tring this code:

ggplot(df aes(x="", y=df$A, fill=A))+
   geom_bar(width = 1, stat = "identity")

The problem is that the labels are overlapping. Also i want to change the labes for 1,2,3 to x,y,z. Here is picture of what is happening enter image description here

And here is a link for the actual data that im using. https://a.uguu.se/anKhhyEv5b7W_Data.csv

Upvotes: 0

Views: 528

Answers (1)

dc37
dc37

Reputation: 16178

Your graph does not correspond to the sample of data you are showing, so it is hard to be sure that the structure of your real data is actually the same.

Using a random example, I get the following plot:

df <- data.frame(A = sample(1:3,20, replace = TRUE))

library(ggplot2)
ggplot(df, aes(x="A", y=A, fill=as.factor(A)))+
  geom_bar(width = 1, stat = "identity")  +
  scale_fill_discrete(labels = c("x","y","z"))

enter image description here

EDIT: Using data provided by the OP

Here using your data, you should get the following plot:

ggplot(df, aes(x = "A",y = A, fill = as.factor(A)))+
  geom_col()

enter image description here

Or if you want the count of each individual values of A, you can do:

library(dplyr)
library(ggplot2)
df %>% group_by(A) %>% count() %>%
  ggplot(aes(x = "A", y = n, fill = as.factor(A)))+
  geom_col()

enter image description here

Is it what you are looking for ?

Upvotes: 2

Related Questions