我使用 csv 文件在 R 中使用 ggplot 生成条形图。但是 x 轴上的标签彼此重叠。 下面是代码:
library(ggplot2)
options(repr.plot.width = 100)
ggplot(data,aes(State,Confirmed))+
geom_bar(stat = "identity")+
请帮我解决这个重叠的问题。
你的函数是 theme()。
这是一个例子:
# 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))