在ggplot绘制此。控制的范围内,y轴的线

问题描述 投票:8回答:1

这是使用的基础上,在哪里可以控制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))

enter image description here

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))

enter image description here

如何添加轴心线酷似基地情节?我曾尝试scale_y_continuousscale_x_continuous但它总是吸引直到情节的结尾。

r plot ggplot2 axis
1个回答
7
投票

你可以到那里使用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))

enter image description here

您也可以手动绘制它们,如果你想:

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))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.