我正在绘制大型数据集的堆叠条形图,效果很好。但是在我的情节中,又产生了一个名为“ NA”的额外条形图。我不知道怎么了,所以请帮助我。而且我还想更改条形的颜色,并使其更加可视化。有什么方法可以更改光谱颜色图案,例如定律值的颜色比暗色更亮。我希望颜色图案为“白色到深蓝色”或任何深色。我的数据集,代码和图表如下:
Degree No.of.vertices Articulationpoint
1 2392 0
2 1439 140
3 981 104
4 698 88
5 579 73
6 445 77
7 366 74
8 272 55
9 255 39
10 226 49
11 179 46
12 146 32
13 121 30
14 124 34
15 95 25
16 88 28
17 96 39
18 70 17
19 81 34
20 64 20
21 59 22
22 45 20
23 41 19
24 38 17
25 28 14
26 30 18
27 29 16
28 28 13
29 22 9
30 18 11
31 16 6
32 15 9
33 17 11
34 14 5
35 25 11
36 14 6
37 8 7
38 19 10
39 9 3
40 14 6
41 9 2
42 9 4
43 10 6
44 7 5
45 8 4
46 3 2
47 5 4
48 10 8
49 8 4
50 3 1
51 5 5
52 5 5
53 8 6
54 4 2
55 3 3
56 3 2
57 6 5
58 2 2
59 6 4
60 2 2
61 5 4
62 5 2
63 3 3
64 5 4
65 1 0
66 3 2
67 3 2
68 1 1
69 2 0
70 6 6
71 2 0
72 4 4
73 5 5
74 7 6
75 1 1
76 1 1
77 2 2
79 1 1
81 1 0
82 1 1
83 2 2
84 4 2
85 2 2
86 1 0
87 1 1
88 2 2
89 2 2
90 2 2
91 1 1
92 1 1
96 3 3
97 1 1
100 1 1
101 2 1
102 2 2
103 1 1
104 1 0
106 1 1
108 2 1
109 1 1
110 1 1
112 1 1
113 2 2
115 2 1
116 1 1
117 2 2
119 1 1
122 1 0
124 1 1
127 2 1
128 1 1
130 1 1
134 2 2
144 1 1
145 1 1
147 1 1
150 1 1
151 1 1
152 2 2
154 1 1
160 1 0
161 1 1
165 1 1
168 1 1
172 1 1
180 1 1
188 1 1
193 1 1
198 1 1
207 1 1
209 1 1
246 1 1
269 1 1
我的代码是:
d <- read.csv("Data.csv");d
df <- data.frame(d);df
df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))
library(reshape2)
library(ggplot2)
ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) +
geom_bar( stat = "identity", position = "stack", colour = "black" )
在我的图表中,我希望白色背景而不是灰色框。而且我也想将图形适合4 * 4框架,所以请帮助我。
请帮助我在此先感谢。
在数据准备中,您正在创建缺失值,这些缺失值将显示在绘图上:
library(dplyr)
# while cutting you are creating missing values
cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134)) %>% tail(., 30)
#> [1] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134]
#> [8] (100,134] (100,134] (100,134] <NA> <NA> <NA> <NA>
#> [15] <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> [22] <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> [29] <NA> <NA>
#> 15 Levels: (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8] ... (100,134]
如果有意义,您可以延长间隔以包括所有数据:
cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134, 270)) %>% tail(., 30)
#> [1] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134]
#> [8] (100,134] (100,134] (100,134] (134,270] (134,270] (134,270] (134,270]
#> [15] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270]
#> [22] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270]
#> [29] (134,270] (134,270]
#> 16 Levels: (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8] ... (134,270]
或者,在绘制之前删除缺失值:
df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))
df <- df[complete.cases(df), ]
可以使用更改背景,方法是通过theme_bw
指定渐变的不同颜色scale_fill_gradient
。
library(ggplot2)
ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) +
geom_bar(stat = "identity", position = "stack", colour = "black" ) +
scale_fill_gradient(high = "#ecf6fe", low = "#085695") + # change low and hig colours as you whish
theme_bw() # white background
就像已经提到的lawyeR一样,保存时可以ggsave
用来控制绘图的长宽比:
ggsave("myplot.png", name_of_plot, width = 4, height = 4)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句