R 如何使ggsave保存的区域更小
假设我使用以下代码创建以下绘图,并使用R 如何使ggsave保存的区域更小,r,dictionary,ggplot2,plot,tidyverse,R,Dictionary,Ggplot2,Plot,Tidyverse,假设我使用以下代码创建以下绘图,并使用ggsave保存: library(tidycensus) library(tidyverse) library(RColorBrewer) options(tigris_use_cache = TRUE) data0 <- get_acs(geography = "county", variables = c(total="B15003_001", phd="B15
ggsave
保存:
library(tidycensus)
library(tidyverse)
library(RColorBrewer)
options(tigris_use_cache = TRUE)
data0 <- get_acs(geography = "county",
variables = c(total="B15003_001", phd="B15003_025"),
year = 2017, geometry = TRUE, keep_geo_vars=TRUE)
data <- data0 %>% filter(STATEFP!="15"&STATEFP!="02"&STATEFP!="72") %>%
select(-moe) %>%
spread(variable, estimate) %>%
mutate(estimate = 100*(phd/total)) %>%
arrange(estimate)
data$GEOID <- as.integer(data$GEOID)
a <-ggplot() +
geom_sf(data = data, aes(fill = cut(estimate, c(0, 1, 2, 4, 20), include.lowest = TRUE)),
size=.005, show.legend = FALSE) +
scale_fill_brewer(type="seq", palette="RdYlGn", name="Percent", direction=-1) +
theme_bw() +[![enter image description here][1]][1]
theme(panel.background = element_rect(fill = 'white')) +
theme(panel.grid = element_blank(),axis.title = element_blank(),
axis.text = element_blank(),axis.ticks = element_blank(),
panel.border = element_blank())
ggsave(a, filename="phd_per.png",width=6,height=3.3,units='in',dpi=400, bg="gray")
我得到这个图像:
我想要的是类似(网格标记除外):
根据评论进行更新
下面是另一个示例,它不需要普查密钥。以上各点保持不变:
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- ggplot(nc) +
geom_sf(aes(fill = AREA))
ggsave(b, filename="nc.png",width=5.35,height=2.1,units='in',dpi=400, bg="gray")
nc能否请您编辑您的问题,以包含不需要API密钥即可访问的示例数据。否则很难测试和帮助你。我真的不知道你想做什么。也许你只需要theme\u void()
,然后使用expand=c(0,0)
与scale\u x\u continuous()
和scale\u y\u continuous()。谢谢
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- ggplot(nc) +
geom_sf(aes(fill = AREA))
ggsave(b, filename="nc.png",width=5.35,height=2.1,units='in',dpi=400, bg="gray")