geom_bar overlapping labels

neto333

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

dc37

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 ?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R: change bar labels in geom_bar

ggplot: geom_bar stacking order and labels

R geom_bar and geom_line labels for grouped bars

Add overall bar and perc labels to geom_bar

Prevent overlapping labels with geom_segment and facet

Overlapping text on top of geom_bar in ggplot2

How to prevent the bars in geom_bar from overlapping with the same variable?

How to remove overlapping bar graph count labels?

R geom_bar and facet_grid labels on top of bars

Adding data labels to geom_bar when using proportions

Put labels over negative and positive geom_bar

R - geom_bar Grouped Barchart with grouped Labels

How to vertically center labels with geom_bar (ggplot2)

R ggplot2 geom_bar add labels with % sign

Vertical vistime timeline and overlapping geom_text labels

How can I prevent geom_text from overlapping labels?

How to show labels in geom_text that is proportional to geom_bar group variable

R: ggplot2 geom_text data labels for binned geom_bar

How to remove select labels in ggplot geom_bar plot and center these labels?

Horizontal geom_bar with no overlaps, equal bar widths, and customized axis tick labels

In ggplot geom_bar, the y axis labels are cluttered at the bottom of bar chart

How to put labels over geom_bar for each bar in R with ggplot2

JFreeChart: Bar chart X axis labels overlapping with large datasets

Prevent two sets of labels from overlapping in a bar chart

Jfree charts Labels overlapping

Highcharts 6.0.2: Overlapping labels

Graphviz overlapping edge labels

Overlapping labels in heatmap plot

How to show only 2 out of 3 labels in the legend of geom_bar