R 强制为ggplot2中的一个面添加x轴标签
我正在按国家绘制数据,每个县都有不同的数据年份范围,因此我在R 强制为ggplot2中的一个面添加x轴标签,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我正在按国家绘制数据,每个县都有不同的数据年份范围,因此我在facet\u wrap(x轴=年)中使用free\u x。一个国家(见附图中的刚果)以年为小数结束。有没有办法在x轴上强制整数或手动编辑单个面的x轴 data %>% filter (Filter > 3 & Region == "Africa") %>% ggplot(mapping = aes(x = year, y = ln_P,
facet\u wrap(x轴=年)
中使用free\u x
。一个国家(见附图中的刚果)以年为小数结束。有没有办法在x轴上强制整数
或手动编辑单个面的x轴
data %>%
filter (Filter > 3 & Region == "Africa") %>%
ggplot(mapping = aes(x = year,
y = ln_P,
group = country)) +
geom_smooth(method = "glm", formula = y ~ poly(x,2), se=FALSE,
aes(weight=N), color = "grey")+
geom_point(aes(shape = Source), size = 2) +
theme_bw()+
theme(text = element_text(family = "Times New Roman"),
legend.direction = "vertical",
legend.position = c(0.8, 0.08),
legend.title = element_text(face="bold"),
strip.text = element_text(face="bold"),
axis.text = element_text(color = "black")) +
labs(y = "ln(P/100-P)", x = "Year") +
scale_y_continuous(name = "ln(P/100-P)", breaks = c(-6, -4, -2, 0)) +
facet_wrap(~country, scales = "free_x")
如果没有数据,很难找到答案: 我想试试这个 1) 安装
scales
,这是一个非常方便的扩展包,如果您以前没有这样做过的话
2) 假设上面的ggplot
图形名为p
:
p + scale_x_continuous(
labels = scales::number_format(accuracy = 1))
如果仍然不工作,请将您的日期
(日期类)更改为年份:
data <- data %>% mutate(year = year(date))
数据%变化(年=年(日期))
再试一次,希望这有帮助 将
年
格式化为日期解决了问题。谢谢大家!
data %>%
filter (Filter>3 & Region == "Africa") %>%
ggplot(mapping = aes(x = as.Date(as.character(year), "%Y"),
y = ln_P,
group = country)) +
geom_smooth(method = "glm",formula = y ~ poly(x,2), se=FALSE, aes(weight=N), color
= "grey")+
geom_point(aes(shape = Source), size = 2) +
theme_bw()+
theme(text = element_text(family = "Times New Roman"),
legend.direction = "vertical",
legend.position = c(0.8, 0.08),
legend.title = element_text(face="bold"),
strip.text = element_text(face="bold"),
axis.text = element_text(color = "black")) +
labs(y = "ln(P/100-P)", x = "Year") +
scale_y_continuous(name = "ln(P/100-P)", breaks = c(-6, -4, -2, 0)) +
facet_wrap(~country,scales = "free_x")
year
变量是否格式化为日期?因为多年来的标签是由ggplot更明智地选择的。