简体   繁体   中英

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在此处输入图片说明

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

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"))

在此处输入图片说明

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()

在此处输入图片说明

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()

在此处输入图片说明

Is it what you are looking for ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM