在ggplot中对齐图例

时间:2019-07-14 16:39:05

标签: r ggplot2

我如何在ggplot中对齐图例,以便获得外观更好的图表?

这是我的代码:

p<-ggplot(df, aes(x=log(debt_GDP), y=log(Tax_Burden))) +
  geom_point(aes(color = Tax_Burden, size=debt_GDP))+  
  labs(color = "Tax Burden\n[% Total\nRevenues]", size = "Public Debt [% GDP]")+
  geom_smooth(method=lm, se=FALSE, linetype="dashed")+
  geom_text_repel(aes(label = cou), size = 2)+
  ylab("Tax Burden (log Scale)") +
  xlab("Public Debt [% GDP] (log Scale)") +
  theme_minimal() +
  theme(text =element_text(family="Times New Roman"),
        legend.background = element_blank(),
        legend.key   = element_blank(),
        legend.text  = element_text(size = 4),
        legend.title = element_text(size=6),
        legend.position=c(0.75, 0.15),
        legend.box = "horizontal", 
        legend.spacing.x = unit(-0.05, 'cm'),
        legend.margin = margin(-0.5,0,0,0, unit="cm"),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x = element_text(size = 8), 
        axis.title.y = element_text(size = 8))+
        guides(colour=guide_legend(nrow=2))+
        guides(size=guide_legend(nrow=2))
print(p)

此代码产生以下图形:

enter image description here

我希望右下角的图例能更好地对齐。就像在它们之间留出更多空间,使自行车处于同一高度。

我应该如何修改代码以实现此目标?

这是我的df

structure(list(cou = c("AUS", "AUT", "BEL", "CAN", "CHE", "CYP", 
"CZE", "DEU", "DNK", "ESP", "EST", "FIN", "FRA", "GBR", "GRC", 
"HKG", "IRL", "ISL", "ISR", "ITA", "JPN", "KOR", "LTU", "LUX", 
"LVA", "MLT", "NLD", "NOR", "NZL", "PRT", "SGP", "SMR", "SVK", 
"SVN", "SWE", "USA"), Tax_Burden = c(6.56658707621864, 7.10378323058756, 
13.5989294077863, 14.5941601053672, 4.23033269686003, 8.01245479184551, 
2.93548615999064, 6.37282255839303, 7.56161328656635, 8.09538109891852, 
0.754031005788082, 4.26250207348529, 5.90023087889293, 8.75078518354052, 
14.9229956403962, 0.305664276885313, 13.1405800807103, 8.982252583037, 
17.1039211691405, 14.9762250144146, 8.63305870207613, 4.37469530072195, 
4.05821367168569, 0.90067003267734, 3.08654443423921, 8.73143621976918, 
8.95036517747877, 4.46674909234817, 6.2098179691248, 14.4984672270715, 
5.49141667845267, 1.1202949642513, 5.72902390182617, 4.5168394983977, 
5.39340451223894, 12.5722562009392), debt_GDP = c(21.2104166497215, 
62.9400759974165, 108.141298492864, 83.6298752648961, 48.4406620192423, 
64.807108331553, 28.6216768283869, 55.7440091450801, 51.0409832496932, 
51.7084437094483, 6.48919561479843, 37.1083399104589, 55.0678477841571, 
47.6829249747454, 122.8633896009, 6.77862326378725, 69.1498628847343, 
48.7390999088899, 113.807320668678, 106.944419479419, 174.822671497659, 
32.242063079036, 29.3504556668286, 11.1090539035687, 20.9308984842423, 
64.9472574924921, 62.4276646515874, 38.5958887770234, 33.9849780826253, 
93.6474319458088, 84.4831946031912, 15.7302630791514, 35.5796518404365, 
34.1291341088789, 56.7774612650971, 67.3050151416759)), row.names = c(NA, 
-36L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

通过使用文本中的"\n"和例如使用legend.spacing.x = unit(0.1, 'cm')对齐两个标题,您可以获得以下内容:

library(ggplot2)
library(ggrepel)
p<-ggplot(df, aes(x=log(debt_GDP), y=log(Tax_Burden))) +
  geom_point(aes(color = Tax_Burden, size=debt_GDP))+  
  labs(color = "Tax Burden\n[% Total Revenues]", size = "Public Debt \n[% GDP]")+
  geom_smooth(method=lm, se=FALSE, linetype="dashed")+
  geom_text_repel(aes(label = cou), size = 2)+
  ylab("Tax Burden (log Scale)") +
  xlab("Public Debt [% GDP] (log Scale)") +
  theme_minimal() +
  theme(text =element_text(family="Times New Roman"),
        legend.background = element_blank(),
        legend.key   = element_blank(),
        legend.text  = element_text(size = 4),
        legend.title = element_text(size=6),
        legend.position=c(0.75, 0.15),
        legend.box = "horizontal", 
        legend.spacing.x = unit(0.1, 'cm'),
        legend.margin = margin(1,0,0,0, unit="cm"),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x = element_text(size = 8), 
        axis.title.y = element_text(size = 8))+
  guides(colour=guide_legend(nrow=2))+
  guides(size=guide_legend(nrow=2))
print(p)

enter image description here