在显示所有x轴值的同时绘制R中的图形

时间:2017-05-28 07:17:43

标签: r plot

我正在尝试使用我的数据绘制图表。 我的代码是

plot(birthRate$country_code, birthRate$yr2014, main = "Birth Rate by Countries 2014")

我的出局是这样的:enter image description here

但我想在x轴上显示所有值。有什么帮助吗?

dput(birthRate)


structure(list(series_code = structure(c(21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L), .Label = c("NY.GNP.PCAP.CD", 
"SE.PRM.ENRR", "SE.SEC.ENRR", "SE.TER.ENRR", "SE.TER.ENRR.FE", 
"SH.ALC.PCAP.LI", "SH.DTH.COMM.ZS", "SH.DTH.INJR.ZS", "SH.DTH.NCOM.ZS", 
"SH.IMM.IBCG", "SH.STA.MMRT.NE", "SH.STA.TRAF.P5", "SH.XPD.PCAP", 
"SH.XPD.PRIV.ZS", "SH.XPD.PUBL.ZS", "SH.XPD.TOTL.ZS", "SL.UEM.TOTL.FE.ZS", 
"SL.UEM.TOTL.MA.ZS", "SL.UEM.TOTL.ZS", "SP.ADO.TFRT", "SP.DYN.CBRT.IN", 
"SP.DYN.CDRT.IN", "SP.DYN.LE00.FE.IN", "SP.DYN.LE00.IN", 
"SP.DYN.LE00.MA.IN", 
"SP.DYN.TFRT.IN"), class = "factor"), country_name = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 14L, 15L, 17L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 28L, 29L), .Label = c("Australia", 
"Brunei Darussalam", "Cambodia", "China", "Fiji", "Indonesia", 
"Japan", "Kiribati", "Korea, Dem. People’s Rep.", "Korea, Rep.", 
"Lao PDR", "Malaysia", "Marshall Islands", "Micronesia, Fed. Sts.", 
"Mongolia", "Nauru", "New Zealand", "Palau", "Papua New Guinea", 
"Philippines", "Samoa", "Singapore", "Solomon Islands", "Thailand", 
"Timor-Leste", "Tonga", "Tuvalu", "Vanuatu", "Vietnam"), class = "factor"), 
country_code = structure(c(1L, 2L, 8L, 3L, 4L, 6L, 7L, 9L, 
20L, 10L, 11L, 14L, 5L, 13L, 16L, 19L, 17L, 29L, 21L, 22L, 
23L, 24L, 25L, 28L, 27L), .Label = c("AUS", "BRN", "CHN", 
"FJI", "FSM", "IDN", "JPN", "KHM", "KIR", "KOR", "LAO", "MHL", 
"MNG", "MYS", "NRU", "NZL", "PHL", "PLW", "PNG", "PRK", "SGP", 
"SLB", "THA", "TLS", "TON", "TUV", "VNM", "VUT", "WSM"), class = "factor"), 
yr2001 = c(12.7, 20.913, 27.327, 13.38, 24.41, 21.486, 9.3, 
30.228, 17.414, 11.6, 30.999, 21.445, 29.21, 19.035, 14.36, 
34.396, 29.301, 30.269, 11.8, 35.403, 14.025, 41.441, 28.365, 
31.84, 17.13), yr2002 = c(12.8, 20.137, 26.793, 12.86, 24.103, 
21.49, 9.3, 29.965, 16.92, 10.2, 30.287, 20.39, 28.453, 19.001, 
13.67, 33.95, 28.892, 29.991, 11.4, 35.226, 13.653, 40.428, 
28.468, 31.219, 16.921), yr2003 = c(12.6, 19.522, 26.44, 
12.41, 23.804, 21.5, 9.2, 29.775, 16.431, 10.2, 29.753, 19.435, 
27.669, 19.209, 13.94, 33.49, 28.404, 29.778, 10.5, 35.061, 
13.32, 39.726, 28.565, 30.597, 16.839), yr2004 = c(12.3, 
19.065, 26.24, 12.29, 23.508, 21.499, 8.6936, 29.647, 15.961, 
9.8, 29.38, 18.62, 26.886, 19.627, 14.2, 33.03, 27.845, 29.624, 
10.3, 34.889, 13.025, 39.368, 28.624, 29.993, 16.848), yr2005 = c(12.8, 
18.738, 26.145, 12.4, 23.208, 21.476, 8.4133, 29.572, 15.532, 
8.9, 29.134, 17.971, 26.139, 20.223, 13.96, 32.575, 27.238, 
29.499, 10.2, 34.68, 12.764, 39.326, 28.611, 29.427, 16.919
), yr2006 = c(12.9, 18.499, 26.098, 12.09, 22.901, 21.429, 
8.65, 29.537, 15.166, 9.2, 28.966, 17.498, 25.461, 20.959, 
14.14, 32.121, 26.619, 29.355, 10.3, 34.409, 12.533, 39.509, 
28.499, 28.92, 17.03), yr2007 = c(14.1, 18.292, 26.043, 12.1, 
22.586, 21.364, 8.63, 29.528, 14.87, 10, 28.821, 17.171, 
24.872, 21.769, 15.15, 31.659, 26.025, 29.148, 10, 34.063, 
12.323, 39.752, 28.288, 28.475, 17.163), yr2008 = c(14, 18.07, 
25.937, 12.14, 22.263, 21.283, 8.7, 29.526, 14.648, 9.4, 
28.651, 16.954, 24.385, 22.576, 15.1, 31.186, 25.489, 28.845, 
10.2, 33.637, 12.123, 39.92, 27.982, 28.091, 17.298), yr2009 = c(13.9, 
17.809, 25.755, 12.13, 21.929, 21.177, 8.5, 29.513, 14.498, 
9, 28.429, 16.828, 24.011, 23.311, 14.53, 30.706, 25.023, 
28.442, 9.9, 33.132, 11.927, 39.95, 27.588, 27.766, 17.409
), yr2010 = c(13.7, 17.499, 25.491, 11.9, 21.583, 21.034, 
8.5, 29.468, 14.411, 9.4, 28.142, 16.773, 23.751, 23.892, 
14.68, 30.229, 24.634, 27.944, 9.3, 32.555, 11.725, 39.8, 
27.112, 27.486, 17.473), yr2011 = c(13.6, 17.146, 25.164, 
11.93, 21.221, 20.841, 8.3, 29.377, 14.374, 9.4, 27.8, 16.765, 
23.598, 24.252, 14, 29.764, 24.315, 27.372, 9.5, 31.918, 
11.51, 39.461, 26.57, 27.236, 17.477), yr2012 = c(13.7, 16.774, 
24.812, 12.1, 20.846, 20.595, 8.2, 29.235, 14.363, 9.6, 27.43, 
16.783, 23.528, 24.378, 13.87, 29.318, 24.041, 26.768, 10.1, 
31.25, 11.281, 38.985, 25.992, 26.993, 17.424), yr2013 = c(13.3, 
16.405, 24.462, 12.08, 20.463, 20.297, 8.2, 29.044, 14.358, 
8.6, 27.051, 16.805, 23.511, 24.275, 13.2, 28.899, 23.79, 
26.172, 9.3, 30.578, 11.041, 38.419, 25.409, 26.739, 17.318
), yr2014 = c(12.9, 16.043, 24.119, 12.4, 20.075, 19.955, 
8, 28.8, 14.349, 8.6, 26.666, 16.811, 23.531, 23.949, 12.68, 
28.51, 23.552, 25.608, 9.8, 29.921, 10.79, 37.783, 24.846, 
26.466, 17.157)), .Names = c("series_code", "country_name", 

“country_code”,“yr2001”,“yr2002”,“yr2003”,“yr2004”,“yr2005”, “yr2006”,“yr2007”,“yr2008”,“yr2009”,“yr2010”,“yr2011”,“yr2012”, “yr2013”​​,“yr2014”),row.names = c(30L,31L,32L,33L,34L,35L, 36L,37L,38L,39L,40L,41L,43L,44L,46L,48L,49L,50L,51L, 52L,53L,54L,55L,57L,58L),class =“data.frame”,na .action = structure(c(13L, 16L,18L,27L),. Name = c(“42”,“45”,“47”,“56”),class =“omit”))

1 个答案:

答案 0 :(得分:0)

您可以尝试水平绘制x轴标签。

试试这个: plot(birthRate$country_code, birthRate$yr2014, main = "Birth Rate by Countries 2014", las=2)

修改

我能够使用ggplot2制作这个条形图。 enter image description here

这是我的代码:

birthRate <- arrange(birthRate, yr2014)
p <- ggplot(birthRate, aes(y=yr2014, x=reorder(country_code, yr2014), fill=country_code)) + 
    geom_col()
p

注意:您的dput()输出对country_code变量有29个观察值,但yr2014变量只有25个。我并不确切知道丢失数据的确切位置,所以我只是从country_code变量中删除了最后四个观察结果以便排列。根据{{​​1}}的位置......

,您的输出可能会略有不同

我希望这有用!