Reputation: 97
I am plotting stacked bar graph of my large data set, it is work fine. But in my plot there is one extra bar is produced as named 'NA'. I don't know what is wrong so please help me. And i also want to change the color of bars and want to make it more visualizing. Is there any way to change the spectral color pattern like the law value's color are more light than dark.I want the color pattern as "white to dark blue" or any dark shade. My dataset,code and plot are as below:
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
My code is:
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" )
In my graph i want white background not that grey boxes. And i also want to fit the graph in 4*4 frame so please help me.
pls help me Thanks in advance.
Upvotes: 0
Views: 138
Reputation: 3311
Inside your data preparation you are creating missing values, which are being displayed on the plot:
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]
You could extend your intervals to include all the data, in case that is meaningful:
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]
Alternatively, remove missing values before plotting:
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), ]
Changing the background can be done with theme_bw
, specifying different colors for the gradient by 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
Like lawyeR already mentioned, you can use ggsave
to control the aspect ratio of your plot when you save it:
ggsave("myplot.png", name_of_plot, width = 4, height = 4)
Upvotes: 1
Reputation: 7654
Using your data and df <- read.table(textConnection("your data")
I did some cleanup (might have used other arguments to read.table, instead).
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))
Then ran your plot code but added theme_bw()
:
p <- ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) + geom_bar( stat = "identity", position = "stack", colour = "black" ) + theme_bw()
This removes the grey background but leaves a massive legend. I am not sure why your outcome differs from mine.
You can set your size dimensions as follows:
ggsave(p, filename = "your plot name.png", width = 4, height = 4, unit = "in")
Upvotes: 1