Overlapping labels on x-axis in ggplot2

49 Views Asked by At

I have used a csv file to generate a bar graph using ggplot in R. But the labels on x-axis are overlapping with each other. below is the code :

library(ggplot2)

options(repr.plot.width = 100)

ggplot(data,aes(State,Confirmed))+
    geom_bar(stat = "identity")+

Please help me to solve this overlapping issue.

enter image description here

1

There are 1 best solutions below

0
MetehanGungor On

Your function is theme().

Here is an example:

# data
data <- data.frame(
  name = c("hey! sample very long text 1", "hey! sample very long text 2", 
         "hey! sample very long text 3", "hey! sample very long text 4", 
         "hey! sample very long text 5", "hey! sample very long text 6",
         "hey! sample very long text 7", "hey! sample very long text 8",
         "hey! sample very long text 9", "hey! sample very long text 10"),  
  value = c(10, 15, 20, 25, 30, 35, 40, 45, 50, 40)
)

# barplot
ggplot(data, aes(x = name, y = value)) + 
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, vjust = .5, hjust = 1))

# or
ggplot(data, aes(x = name, y = value)) + 
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))