2017-03-01 42 views
8

这是使用基准,我可以在其中控制x和y轴范围,准确地绘制线条。将其绘制在ggplot中。在一个范围内控制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,但它总是绘制直到剧情结束。

回答

6

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

+0

谢谢, 一世已经尝试过(使用theme_hc())。但我想用ggthemes做这个。可能吗 ? – PoisonAlien

+0

我想你可以在缩放比例缩放时手动绘制这些片段。 – Axeman

+0

是的,那是我想到的。我会尝试。令人惊讶的是,ggplot正在成熟,没有选择。 – PoisonAlien