堆积条形图

时间:2015-10-11 19:57:46

标签: r ggplot2 reshape2

我正在绘制我的大数据集的堆积条形图,它工作正常。但是在我的情节中,有一个额外的条形被命名为'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" )

导致: enter image description here

在我的图表中,我希望白色背景不是那个灰色的盒子。我还希望将图形放在4 * 4帧中,所以请帮助我。

请帮助我 提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用你的数据和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&lt; - ggplot(df,aes(x = Degree,y = No.of.vertices,fill = Articulationpoint))+   geom_bar(stat =“identity”,position =“stack”,color =“black”)+   theme_bw()

这删除了灰色背景,但留下了一个巨大的传说。我不确定你的结果与我的结果有什么不同。

reproduced plot, sort of

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

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

答案 1 :(得分:1)

在数据准备工作中,您正在创建缺失值,这些值将显示在图中:

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)