Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 堆积条形图_R_Ggplot2_Reshape2 - Fatal编程技术网

R 堆积条形图

R 堆积条形图,r,ggplot2,reshape2,R,Ggplot2,Reshape2,我正在绘制我的大数据集的堆叠条形图,工作正常。但在我的情节中,有一个额外的酒吧被命名为“NA”。我不知道怎么了,请帮帮我。我还想改变条形图的颜色,让它更直观。是否有任何方法可以改变光谱颜色模式,例如,law值的颜色更亮而不是更暗。我希望颜色模式为“白色到深蓝色”或任何深色。我的数据集、代码和绘图如下: Degree No.of.vertices Articulationpoint 1 2392 0 2 1439 140 3 981 104 4 698 88 5

我正在绘制我的大数据集的堆叠条形图,工作正常。但在我的情节中,有一个额外的酒吧被命名为“NA”。我不知道怎么了,请帮帮我。我还想改变条形图的颜色,让它更直观。是否有任何方法可以改变光谱颜色模式,例如,law值的颜色更亮而不是更暗。我希望颜色模式为“白色到深蓝色”或任何深色。我的数据集、代码和绘图如下:

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" )

d使用数据和
df在数据准备中,您正在创建缺失的值,这些值显示在绘图上:

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]
或者,在打印前删除缺少的值:

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), ]


如前所述,您可以使用
ggsave
在保存绘图时控制绘图的纵横比:

ggsave("myplot.png", name_of_plot, width = 4, height = 4)

非常感谢律师和托马斯K。
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), ]
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
ggsave("myplot.png", name_of_plot, width = 4, height = 4)