在ggplot中设置y轴的标签之间的距离

时间:2020-08-29 14:14:34

标签: r ggplot2

我在下面创建了气泡图,但是正如您所看到的,我在y轴上有太多无法区分的名称。如何扩大它们之间的距离以使其更明显?

library(ggplot2)
library(dplyr)

# The dataset is provided in the gapminder library
library(gapminder)
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)

# Most basic bubble plot
data %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country)) %>%
  ggplot(aes(x=gdpPercap, y=country, size=pop, color=continent)) +
  geom_point(alpha=0.5) +
  scale_size(range = c(.1, 24), name="Population (M)")+ facet_grid(cols = vars(continent))+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))

enter image description here

2 个答案:

答案 0 :(得分:1)

即使您按大陆分组,您的问题也来自于在同一轴上显示所有国家。您应该将facet_grid()的{​​{1}}切换为facet_wrap()。然后,您可以保存图像并设置高度和宽度以获取所需的图像。我在下面的图片中使用的尺寸为高度= 800,宽度= 2000。

scales = "free_y"

enter image description here

答案 1 :(得分:1)

我建议您使用一种方法来避开y轴标签,但暗示您必须设置字体大小。您必须将 public static String getHotspotIpAddress(Context context) { WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE); DhcpInfo dhcpInfo = wifimanager == null ? null : wifimanager.getDhcpInfo(); if (dhcpInfo != null) { int address = dhcpInfo.serverAddress; return ((address & 0xFF) + "." + ((address >> 8) & 0xFF) + "." + ((address >> 16) & 0xFF) + "." + ((address >> 24) & 0xFF)); } return ""; } 添加到guide = guide_axis(n.dodge=2)选项。您可以玩几个想要的躲避级别。这里的代码:

scale_y_discrete()

输出:

enter image description here