相同的y轴,但不同的x轴图-

时间:2018-11-19 20:56:55

标签: r ggplot2 graph

  • 如何更改此图,以便在所有三个子图中都有一个图例,但x和y轴的大小相同?

A graph with the same y-axis but different x-axis.

这是我的代码:

eu <- ggplot(regions_dist6, aes(x=log(pop_density1), y=log(ET_pc_y))) + geom_point(aes(colour = urb_lvl, size=car_dw_share),  alpha = 0.2)
eu1 <- eu + scale_color_gradient(low="blue", high="red", name = "% Urbanization") 
eu2 <- eu1 + geom_smooth(span = .95) + ylab (bquote('log Commuting emissions ('*kgCO[2]/cap/year*')')) + 
  xlab (bquote('log Population density '*(p/km^2)*'')) 
eu3 <- eu2 + labs(size = "% 4-Wheelers") + theme_light()

ed <- ggplot(regions_dist6, aes(x=log(utm44_RoadDens_km_sqkm), y=log(ET_pc_y))) + geom_point(aes(colour = urb_lvl, size=car_dw_share),  alpha = 0.2)
ed1 <- ed + scale_color_gradient(low="blue", high="red", name = "% Urbanization") 
ed2 <- ed1 + geom_smooth(span = .95) + ylab (bquote('log Commuting emissions ('*kgCO[2]/cap/year*')')) + 
  xlab (bquote('log Road density '*(km/km^2)*''))
ed3 <- ed2 + labs(size = "% 4-Wheelers") + theme_light()

eg <- ggplot(regions_dist6, aes(x=log(gdp_pc_inr), y=log(ET_pc_y))) + geom_point(aes(colour = urb_lvl, size=car_dw_share),  alpha = 0.2)
eg1 <- eg + scale_color_gradient(low="blue", high="red", name = "% Urbanization") 
eg2 <- eg1 + geom_smooth(span = .95) + ylab (bquote('log Commuting emissions ('*kgCO[2]/cap/year*')')) + 
  xlab (bquote('log GDP (₹/c)'))
eg3 <- eg2 + labs(size = "% 4-Wheelers") + theme_light()

grid.newpage()
pushViewport(viewport(layout = grid.layout(1,3)))

print(eu3, vp=viewport(layout.pos.col = 1, layout.pos.row = 1))
print(ed3, vp=viewport(layout.pos.col = 2, layout.pos.row = 1))
print(eg3, vp=viewport(layout.pos.col = 3, layout.pos.row = 1))

这里是数据链接:50个观测值 https://www.dropbox.com/s/6lrk4a5lo7l2nnj/regions_dist6a.csv?dl=0

1 个答案:

答案 0 :(得分:0)

考虑到joran的建议,我提出了以下建议:

library(tidyverse)

#load dataset
regions_dist6 <- read.csv("test.csv")

#create category-value columns for the facet_grid
reg_melt <- regions_dist6 %>%
        gather(categ, categ_val, c("pop_density", "UTM44_Area_km2", "gdp_pc_inr"))
#create labels for facet_grid
xlabels <- c(pop_density = "log population density", UTM44_Area_km2 = "log road density", gdp_pc_inr = "log GDP") 


eu <- ggplot(reg_melt, aes(x=log(categ_val), y=log(ET_pc_y))) +
        #facet grid for the predefined categories with autoscaled x-axes
        facet_grid(cols = vars(categ), scales = "free_x", labeller = as_labeller(xlabels)) +
        geom_point(aes(colour = urb_lvl, size=car_dw_share),  alpha = 0.2) +
        scale_color_gradient(low="blue", high="red", name = "% Urbanization") + 
        geom_smooth(span = .95) + 
        ylab (bquote('log Commuting emissions ('*kgCO[2]/cap/year*')')) + 
        #remove automatic xlabel
        xlab (NULL) +
        labs(size = "% 4-Wheelers") + 
        theme_light()

x11(xpos = 50, ypos = 50)
print(eu)

使用数据采样输出: enter image description here

由于我只是开始探索R,因此您最好仔细检查一下是否符合您的想法。而且我没有找到一种方法来单独标记facet_grid的轴(我只是删除了所有三个x轴的通用标签),因此,如果有经验的人提出了更好的解决方案,很高兴为它投票。