Poi*_*ien 8 plot axis r ggplot2
这是使用base,我可以在其中控制x和y轴范围,其中应该精确绘制线条.
plot(mtcars$mpg, mtcars$hp, ylim = c(0, 400), xlim = c(0, 50), axes = F, xlab = 'mpg', ylab = 'hp', pch = 16)
axis(side = 2, at = seq(100, 400, 100))
axis(side = 1, at = seq(10, 30, 10))
Run Code Online (Sandbox Code Playgroud)
ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+
theme(panel.background = element_blank())+
scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50))+
scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400))
Run Code Online (Sandbox Code Playgroud)
如何像基本曲线一样添加轴线?我已经尝试了scale_y_continuous,scale_x_continuous但它总是画到情节结束.
你可以使用这个ggthemes包到达那里:
library(ggthemes)
ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+
geom_rangeframe(data = data.frame(mpg = c(10, 30), hp = c(100, 400))) +
theme_tufte() +
scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50))+
scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400))
Run Code Online (Sandbox Code Playgroud)
如果需要,您也可以手动绘制它们:
ggplot(data = mtcars, aes(x = mpg, y = hp))+geom_point()+
geom_segment(aes_all(c('x', 'y', 'xend', 'yend')),
data = data.frame(x = c(0, 10), xend = c(0, 30), y = c(100, 0), yend = c(400, 0))) +
theme(panel.background = element_blank()) +
scale_x_continuous(breaks = seq(10, 30, 10), limits = c(0, 50), expand = c(0, 0))+
scale_y_continuous(breaks = seq(100, 400, 100), limits = c(0, 400), expand = c(0, 0))
Run Code Online (Sandbox Code Playgroud)