我正在使用條形圖,并嘗試調整標簽(位置,在我的資料集中),以便它們與 x 軸刻度線對齊。資料如下:
data <- data.frame(location=c('Allen',
'Allen',
'Anderson',
'Atchison',
'Barber',
'Barber',
'Bourbon',
'Brown',
'Brown',
'Butler',
'Butler',
'Butler',
'Butler',
'Cherokee',
'Cherokee',
'Cheyenne',
'Comanche',
'Cowley',
'Crawford',
'Crawford',
'Crawford',
'Decatur',
'Dickinson',
'Dickinson',
'Dickinson',
'Doniphan',
'Douglas',
'Douglas',
'Douglas',
'Douglas',
'Edwards',
'Edwards',
'Ellis',
'Ellis',
'Finney',
'Finney',
'Finney',
'Ford',
'Geary',
'Gove',
'Graham',
'Grant',
'Gray',
'Harper',
'Harper',
'Harvey',
'Harvey',
'Harvey',
'Jackson',
'Jefferson',
'Jefferson',
'Jefferson',
'Jefferson',
'Jewell',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Johnson',
'Labette',
'Labette',
'Labette',
'Lane',
'Leavenworth',
'Leavenworth',
'Leavenworth',
'Leavenworth',
'Leavenworth',
'Leavenworth',
'Linn',
'Lyon',
'Marion',
'Marion',
'Marshall',
'Marshall',
'Meade',
'Miami',
'Miami',
'Miami',
'Miami',
'Miami',
'Miami',
'Montgomery',
'Montgomery',
'Montgomery',
'Montgomery',
'Montgomery',
'Morris',
'Nemaha',
'Neosho',
'Ness',
'Osage',
'Osage',
'Pottawatomie',
'Pottawatomie',
'Pottawatomie',
'Reno',
'Reno',
'Reno',
'Reno',
'Reno',
'Reno',
'Republic',
'Rice',
'Riley',
'Riley',
'Riley',
'Rooks',
'Rush',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Sedgwick',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Shawnee',
'Sheridan',
'Smith',
'Stanton',
'Sumner',
'Sumner',
'Trego',
'Wabaunsee',
'Wilson',
'Wilson',
'Woodson',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte',
'Wyandotte'),
vehicles = c(43472,
51429,
53577,
54936,
56705,
63875,
46172,
54135,
53690,
72173,
93634,
83378,
72500,
46458,
53843,
55352,
58173,
78182,
56406,
45114,
74543,
43646,
68798,
57917,
46219,
62188,
53445,
79500,
86150,
63523,
55598,
58438,
65673,
71417,
73958,
117703,
36750,
52349,
50906,
56667,
67292,
65000,
67381,
54044,
49648,
55774,
78272,
67056,
69962,
73036,
74688,
63177,
66189,
51932,
67266,
27860,
72358,
106582,
72923,
71667,
115741,
97734,
113750,
98359,
0,
49193,
48553,
45313,
64805,
76667,
90597,
95944,
88431,
74613,
75625,
54848,
74750,
65057,
63824,
54250,
55651,
61797,
90083,
96450,
91549,
79722,
74543,
66111,
55865,
43750,
46310,
62411,
58750,
52838,
62813,
60972,
63125,
64495,
54492,
82574,
68850,
57557,
41100,
70313,
43152,
62482,
59155,
47713,
46613,
66583,
42853,
65773,
0,
54531,
52750,
25455,
28889,
55682,
52926,
43646,
26857,
34224,
81167,
81917,
79556,
86826,
80326,
43333,
84325,
103333,
78279,
97940,
71085,
91146,
98008,
83750,
44554,
78065,
75500,
84615,
76004,
88480,
80392,
65472,
77957,
90848,
59342,
49821,
50703,
60147,
73897,
57017,
68966,
48750,
45924,
50568,
0,
22399,
30625,
29676,
36250,
0,
0,
66806,
41714,
0,
0,
106574,
110909,
73487,
97083))
這是我用來生成圖表的代碼:
library(ggplot2)
ggplot(data = data, aes(x = location, y = vehicles))
geom_bar(stat = 'identity', fill = 'steelblue')
theme(axis.text.x = element_text(angle = 90))
xlab("Location")
ylab("Vehicles")
ggtitle("Vehicles per location")
如下所示,標簽與刻度線不匹配。我需要將它們稍微向左移動。我怎樣才能解決這個問題?

uj5u.com熱心網友回復:
如果你在里面設定hjust,你可以隨意調整位置:vjusttheme(axis.text.x = element_text(...))
library(ggplot2)
ggplot(data = data, aes(x = location, y = vehicles))
geom_bar(stat = 'identity', fill = 'steelblue')
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1))
xlab("Location")
ylab("Vehicles")
ggtitle("Vehicles per location")

uj5u.com熱心網友回復:
這是對艾倫明智答案的補充,但我只是想添加一個可能不太廣為人知的選項,您可以在軸指南中設定角度,它會自動為您調整hjust/ vjust。尤其是像 45 度這樣的角度,比使用主題更容易。
library(ggplot2)
ggplot(data = data, aes(x = location, y = vehicles))
geom_bar(stat = 'identity', fill = 'steelblue')
guides(x = guide_axis(angle = 90))
xlab("Location")
ylab("Vehicles")
ggtitle("Vehicles per location")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427664.html
上一篇:創建GGPLOT直方圖
