ggplot2:双Y轴和黄土平滑问题

时间:2018-10-06 18:02:34

标签: r ggplot2 multiple-axes

我是R和ggplot的新手。我认识到R的力量和ggplot的优雅,并且正在尝试学习。通常,我可以在网上找到解决方案,但是这次没有运气。

我正在尝试使用ggplot生成图表,使用世界银行的数据将经济自由度得分与预期寿命和婴儿死亡率进行比较(csv数据位于文章底部)。使用此代码(在https://rpubs.com/MarkusLoew/226759的示例)我取得了一些成功:

p <- ggplot(mydata, aes(x = Score))
  p <- p + geom_point(aes(y = Longevity, colour = "Life Expectancy")) 
  p <- p + geom_point(aes(y = Infant/1, colour = "Infant mortality (per 
capita)")) 
   p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Infant 
mortality (per capita)")) 
  p <- p + scale_colour_manual(values = c("blue", "red"))
  p <- p + labs(y = "Life Expectancy (years)",
                x = "Score",
                colour = " ")
p

这产生了以下内容: my messed up chart

enter image description here

我无法正确缩放主要的y轴。按示例(上面的链接)缩放图形不起作用:我只是扩展或压缩Longevity数据。我尝试在次要y上加载Longevity数据,但仍然无法正常工作。

另一个问题是我想向每组数据添加LOESS平滑趋势线。我尝试了以下各种示例,但没有用。

如果有人有解决方案,将不胜感激!

谢谢

数据:

Country Name,Score,GDP,Infant,Longevity,,,,,,,,,
Afghanistan,48.9,585.850064,53.2,63.673,,,,,,,,,
Albania,64.4,4537.86249,8.1,78.345,,,,,,,,,
Algeria,46.5,4.12E+03,21,76.078,,,,,,,,,
Angola,48.5,4.17E+03,55.8,61.547,,,,,,,,,
Argentina,50.4,1.44E+04,9.7,76.577,,,,,,,,,
Armenia,70.3,3936.79832,11.9,74.618,,,,,,,,,
Australia,81,5.38E+04,3.1,82.5,,,,,,,,,
Austria,72.3,4.73E+04,3,80.8902439,,,,,,,,,
Azerbaijan,63.6,4131.61831,21.9,72.026,,,,,,,,,
Bahrain,68.5,23655.0356,6.4,76.9,,,,,,,,,
Bangladesh,55,1.52E+03,28.3,72.489,,,,,,,,,
Barbados,54.5,16788.6839,11.9,75.906,,,,,,,,,
Belarus,58.6,5726.02967,2.9,73.82682927,,,,,,,,,
Belgium,67.8,4.33E+04,3.1,80.99268293,,,,,,,,,
Belize,58.6,4905.50628,12.8,70.384,,,,,,,,,
Benin,59.2,829.797231,65.1,60.907,,,,,,,,,
Bhutan,58.4,3110.23011,26.5,70.197,,,,,,,,,
Bolivia,47.7,3393.95582,29,69.125,,,,,,,,,
Bosnia and Herzegovina,60.2,5180.6363,5.1,76.911,,,,,,,,,
Botswana,70.1,7595.59585,32.3,66.797,,,,,,,,,
Brazil,52.9,9.82E+03,14.6,75.509,,,,,,,,,
Brunei Darussalam,69.8,28290.5852,9,77.203,,,,,,,,,
Bulgaria,67.9,8031.59844,6.7,74.61463415,,,,,,,,,
Burkina Faso,59.6,670.705913,52.6,60.361,,,,,,,,,
Burundi,53.2,320.08687,44.1,57.481,,,,,,,,,
Cabo Verde,56.9,3209.69112,15.9,72.798,,,,,,,,,
Cambodia,59.5,1384.42319,26.3,68.981,,,,,,,,,
Cameroon,51.8,1446.70289,56.6,58.073,,,,,,,,,
Canada,78.5,4.50E+04,4.6,82.3005122,,,,,,,,,
Central African Republic,51.8,418.411287,89.2,52.171,,,,,,,,,
Chad,49,669.886426,75,52.903,,,,,,,,,
Chile,76.5,1.53E+04,6.6,79.522,,,,,,,,,
China,57.4,8.83E+03,8.6,76.252,,,,,,,,,
Colombia,69.7,6.30E+03,13.1,74.381,,,,,,,,,
Comoros,55.8,797.286368,53.6,63.701,,,,,,,,,
Costa Rica,65,11630.6684,8,79.831,,,,,,,,,
Cote d'Ivoire,63,1662.44247,66,53.582,,,,,,,,,
Croatia,59.4,13294.5149,4,78.02195122,,,,,,,,,
Cyprus,67.9,25233.571,2.2,80.508,,,,,,,,,
Czech Republic,73.3,2.04E+04,2.6,78.33170732,,,,,,,,,
Denmark,75.1,5.63E+04,3.7,80.70487805,,,,,,,,,
Djibouti,46.7,1927.58971,53,62.465,,,,,,,,,
Dominica,63.7,7609.61435,30.4,,,,,,,,,,
Dominican Republic,62.9,7052.25884,25.6,73.861,,,,,,,,,
Ecuador,49.3,6.20E+03,12.7,76.327,,,,,,,,,
"Egypt, Arab Rep.",52.6,2.41E+03,19.4,71.484,,,,,,,,,
El Salvador,64.1,3889.30877,12.9,73.512,,,,,,,,,
Equatorial Guinea,45,9850.01358,67.4,57.681,,,,,,,,,
Estonia,79.1,19704.655,2.3,77.73658537,,,,,,,,,
Ethiopia,52.7,767.563478,42.5,65.475,,,,,,,,,
Fiji,63.4,5589.38883,21.1,70.269,,,,,,,,,
Finland,74,4.57E+04,1.9,81.7804878,,,,,,,,,
France,63.3,3.85E+04,3.5,82.27317073,,,,,,,,,
Gabon,58.6,7220.68724,36.1,66.105,,,,,,,,,
Georgia,76,4078.25488,10.2,73.261,,,,,,,,,
Germany,73.8,4.45E+04,3.2,80.64146341,,,,,,,,,
Ghana,56.2,1641.48662,37.2,62.742,,,,,,,,,
Greece,55,1.86E+04,4.2,81.03658537,,,,,,,,,
Guatemala,63,4470.98957,23.9,73.409,,,,,,,,,
Guinea,47.6,825.34493,58.1,60.015,,,,,,,,,
Guinea-Bissau,56.1,723.658622,57.4,57.403,,,,,,,,,
Guyana,58.5,4725.31906,26.7,66.65,,,,,,,,,
Haiti,49.6,765.683925,55,63.33,,,,,,,,,
Honduras,58.8,2480.12593,16.2,73.575,,,,,,,,,
"Hong Kong SAR, China",88.6,4.62E+04,,84.22682927,,,,,,,,,
Hungary,65.8,1.42E+04,4.1,75.56829268,,,,,,,,,
Iceland,74.4,70056.8734,1.7,82.46829268,,,,,,,,,

1 个答案:

答案 0 :(得分:0)

这应该为您提供一个良好的开端。如果愿意,可以和 Day Jan. Feb. Mar. Apr. May Jun. Jul. Aug. Sep. Oct. Nov. Dec. Year 0 1 118.0 99.3 85.9 75.5 119 182 656 2790.0 1690.0 402.0 232.0 158.0 2001 1 2 123.0 97.4 82.9 74.3 134 251 514 2420.0 2180.0 397.0 230.0 158.0 2001 2 3 118.0 95.5 80.7 73.1 168 377 466 2190.0 2190.0 386.0 226.0 157.0 2001 ----------------------------------------------- Skipping some rows of no real interest 28 29 95.5 71.9 93.2 165.0 446 1670 2140 410.0 244.0 163.0 118.0 NaN 2001 29 30 98.4 76.0 109.0 176.0 575 2040 2100 403.0 239.0 159.0 117.0 NaN 2001 30 31 98.4 75.1 174.0 3330.0 1600 234 117 NaN NaN NaN NaN NaN 2001 scale_ratio一起玩

dif

计算缩放两个y轴所需的比率

library(tidyverse)

mydata <- read_csv(text, col_types = paste0(c("c", rep("d", 4), rep("_", 9)), collapse = ""))
mydata
#> # A tibble: 67 x 5
#>    `Country Name` Score    GDP Infant Longevity
#>    <chr>          <dbl>  <dbl>  <dbl>     <dbl>
#>  1 Afghanistan     48.9   586.   53.2      63.7
#>  2 Albania         64.4  4538.    8.1      78.3
#>  3 Algeria         46.5  4120    21        76.1
#>  4 Angola          48.5  4170    55.8      61.5
#>  5 Argentina       50.4 14400     9.7      76.6
#>  6 Armenia         70.3  3937.   11.9      74.6
#>  7 Australia       81   53800     3.1      82.5
#>  8 Austria         72.3 47300     3        80.9
#>  9 Azerbaijan      63.6  4132.   21.9      72.0
#> 10 Bahrain         68.5 23655.    6.4      76.9
#> # ... with 57 more rows

添加拟合线及其对应的方程/ R2

scale_ratio <- (max(mydata$Infant, na.rm = TRUE) - min(mydata$Infant, na.rm = TRUE)) /
  (max(mydata$Longevity, na.rm = TRUE) - min(mydata$Longevity, na.rm = TRUE))

dif <-  min(mydata$Longevity, na.rm = TRUE) - min(mydata$Infant, na.rm = TRUE)

myColor <- c("#d95f02", "#1b9e77")

p <- ggplot(mydata, aes(x = Score, y = Longevity)) + 
  geom_point(aes(colour = "Life Expectancy"), 
             shape = "triangle",
             alpha = 0.7, size = 2) +
  geom_point(aes(y = Infant/scale_ratio + dif, 
                 colour = "Infant mortality (per capita)"), 
             alpha = 0.7, size = 2) +
  scale_y_continuous(sec.axis = sec_axis(~ (. - dif) * scale_ratio, 
                                         name = "Infant mortality (per capita)")) +
  scale_colour_manual(values = myColor) +
  theme_bw(base_size = 14) +
  labs(y = "Life Expectancy (years)", 
       x = "Score",
       colour = " ") +
  guides(colour = guide_legend(title = "",
                              override.aes = list(shape = c("circle", "triangle")))) +
  theme(legend.position = 'bottom') +
  NULL
p

reprex package(v0.2.1.9000)于2018-10-07创建

相关问题