堆积条形图

Stacked bar plot

我正在绘制大型数据集的堆叠条形图,效果很好。但在我的情节中,有一个额外的柱被生成,名为 'NA'。我不知道出了什么问题所以请帮助我。而且我还想更改条形图的颜色并使其更加直观。有什么方法可以改变光谱颜色模式,比如法则值的颜色比 dark.I 想要的颜色模式更亮 "white to dark blue" 或任何深色。我的数据集、代码和绘图如下:

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 的框架中,所以请帮助我。

请帮助我 提前致谢。

使用您的数据和 df <- read.table(textConnection("your data") 我做了一些清理(可能使用其他参数 read.table)。

df <- as.data.frame(df)
colnames(df) <- c("Degree", "No.of.vertices", "Articulationpoint")
df$Degree <- as.numeric(df$Degree)
df$No.of.vertices <- as.numeric(df$No.of.vertices)

df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))

然后运行你的剧情代码但是添加了theme_bw():

p <- ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) + geom_bar( 统计 = "identity", 位置 = "stack", 颜色 = "black" ) + theme_bw()

这删除了灰色背景,但留下了巨大的图例。我不确定为什么你的结果与我的不同。

您可以按如下方式设置尺寸:

ggsave(p, filename = "your plot name.png", width = 4, height = 4, unit = "in")

在您的数据准备过程中,您正在创建缺失值,这些值显示在图中:

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)