Home > Software engineering >  R ggplot() messing up the data
R ggplot() messing up the data

Time:02-24

So, I'm trying to build a horizontal barplot on R, as follows:

influencers=c(rep(row.names(data),4))
networks=c(rep("Youtube",60),rep("Twitter",60),rep("Instagram",60),rep("Facebook",60))
followers=c(f.you,f.twi,f.ins,f.fac)/1000000
bars=data.frame(influencers,networks,followers)

ggplot(bars, aes(fill=networks, y=followers, x=reorder(influencers,followers)))   
  geom_bar(position="stack", stat="identity") 
  coord_flip() 
  ylab("Followers (millions)")  
  xlab("")  
  theme_bw()

But the resulting plot is totally messed up, there are some bars with entries that simply doesn't exist on my data. I guess the only data that's correct is the YouTube... Check this out:

enter image description here

I've used a "*" on some of the wrong data.

I don't know what to do. I've double checked the data frame (the data are paired correctly), the values seems to be right, I replace any NA with 0, now I'm trying to reorder like this:

ggplot(bars, aes(reorder(influencers,as.numeric(gsub('([0-9] ).*','\\1',influencers))),followers,fill=networks))  
  coord_flip()  
  geom_bar(position="stack", stat="identity") 
  theme_bw()

But still unsuccessfully, sadly... Someone help me, please.

The data frame:

influencers     networks followers
1          Gene   Youtube  0.102000
2       Yoonjoo   Youtube  0.199000
3         Bruce   Youtube  0.061000
4        Lauryn   Youtube  0.069100
5           Eli   Youtube  0.007280
6      Danielle   Youtube  0.219000
7       Makayla   Youtube  0.025000
8         Jacky   Youtube  0.156000
9         Kelly   Youtube  0.029300
10       Olivia   Youtube  1.010000
11         Sahl   Youtube  1.100000
12         Kiel   Youtube  0.164000
13    Shantiana   Youtube  0.324000
14        Belle   Youtube  2.500000
15       Jeremy   Youtube  0.045300
16       Jaasir   Youtube  0.010900
17      Zuhaira   Youtube  0.029700
18       Khaira   Youtube  0.013000
19      Chelsea   Youtube  3.150000
20      Viviana   Youtube  0.097300
21     Muzammil   Youtube  0.088300
22     Gabriela   Youtube  0.474000
23       Isaiah   Youtube  0.034800
24    Anjanetta   Youtube  0.019200
25      Dimitri   Youtube  0.024800
26    Abbigelle   Youtube  0.007160
27        Hinna   Youtube  0.025700
28        Ethan   Youtube  0.397000
29      Ashante   Youtube 15.800000
30      Heather   Youtube  0.212000
31       Johney   Youtube  0.043500
32        Falon   Youtube  2.170000
33        Kylie   Youtube  0.105000
34       Alicia   Youtube  0.133000
35      Xochitl   Youtube  0.731000
36       Jaasir   Youtube  1.420000
37        Amber   Youtube  0.005140
38      Brandon   Youtube  0.152000
39         Rana   Youtube  0.268000
40      Jameela   Youtube  0.014300
41      Breanna   Youtube  1.150000
42       Victor   Youtube  0.048400
43      Mckenna   Youtube  0.085700
44      Matthew   Youtube  0.740000
45      Chelsea   Youtube  0.084200
46        Blake   Youtube  0.127000
47     Jordynne   Youtube  0.098500
48        Sarah   Youtube  0.065900
49      Desmond   Youtube  0.024200
50    Alexandra   Youtube  1.080000
51      Mahboob   Youtube  0.045200
52         Xiao   Youtube  0.104000
53        Julio   Youtube  0.017300
54        Numa   Youtube  0.013800
55       Daniel   Youtube  0.304000
56       Keisha   Youtube  0.010100
57    Shaquille   Youtube  0.008310
58         Kara   Youtube  0.020500
59       Lauren   Youtube  0.015800
60        Kevin   Youtube  0.184000
61         Gene   Twitter  0.017600
62      Yoonjoo   Twitter  0.005412
63        Bruce   Twitter  0.014800
64       Lauryn   Twitter  0.016200
65          Eli   Twitter  0.000619
66     Danielle   Twitter  0.010000
67      Makayla   Twitter  0.000394
68        Jacky   Twitter  0.044000
69        Kelly   Twitter  0.004881
70       Olivia   Twitter  0.220100
71         Sahl   Twitter  0.066500
72         Kiel   Twitter  0.038400
73    Shantiana   Twitter  0.075900
74        Belle   Twitter  0.171100
75       Jeremy   Twitter  0.006399
76       Jaasir   Twitter  0.000000
77      Zuhaira   Twitter  0.005434
78       Khaira   Twitter  0.024100
79      Chelsea   Twitter  0.398300
80      Viviana   Twitter  0.059900
81     Muzammil   Twitter  0.009801
82     Gabriela   Twitter  0.000926
83       Isaiah   Twitter  0.000276
84    Anjanetta   Twitter  0.000027
85      Dimitri   Twitter  0.000405
86    Abbigelle   Twitter  0.004262
87        Hinna   Twitter  0.000048
88        Ethan   Twitter  0.069400
89      Ashante   Twitter  0.154200
90      Heather   Twitter  0.027100
91       Johney   Twitter  0.006558
92        Falon   Twitter  0.009579
93        Kylie   Twitter  0.010700
94       Alicia   Twitter  0.016400
95      Xochitl   Twitter  0.002992
96       Jaasir   Twitter  0.008854
97        Amber   Twitter  0.001494
98      Brandon   Twitter  0.022600
99         Rana   Twitter  0.104800
100     Jameela   Twitter  0.000000
101     Breanna   Twitter  0.014100
102      Victor   Twitter  0.011600
103     Mckenna   Twitter  0.016200
104     Matthew   Twitter  0.015000
105     Chelsea   Twitter  0.000467
106       Blake   Twitter  0.007961
107    Jordynne   Twitter  0.010400
108       Sarah   Twitter  0.067400
109     Desmond   Twitter  0.016100
110   Alexandra   Twitter  0.196800
111     Mahboob   Twitter  0.001396
112        Xiao   Twitter  0.000423
113       Julio   Twitter  0.004294
114       Numa   Twitter  0.004294
115      Daniel   Twitter  0.002621
116      Keisha   Twitter  0.005747
117   Shaquille   Twitter  0.013600
118        Kara   Twitter  0.001728
119      Lauren   Twitter  0.018700
120       Kevin   Twitter  0.254200
121        Gene Instagram  0.023200
122     Yoonjoo Instagram  0.013500
123       Bruce Instagram  0.004958
124      Lauryn Instagram  0.006026
125         Eli Instagram  0.001726
126    Danielle Instagram  0.013700
127     Makayla Instagram  0.005150
128       Jacky Instagram  0.013200
129       Kelly Instagram  0.000932
130      Olivia Instagram  0.164000
131        Sahl Instagram  0.058100
132        Kiel Instagram  0.015000
133   Shantiana Instagram  0.250000
134       Belle Instagram  0.159000
135      Jeremy Instagram  0.005613
136      Jaasir Instagram  0.024100
137     Zuhaira Instagram  0.002463
138      Khaira Instagram  0.012100
139     Chelsea Instagram  1.100000
140     Viviana Instagram  0.012900
141    Muzammil Instagram  0.000000
142    Gabriela Instagram  0.123000
143      Isaiah Instagram  0.000000
144   Anjanetta Instagram  0.000000
145     Dimitri Instagram  0.007311
146   Abbigelle Instagram  0.000000
147       Hinna Instagram  0.000550
148       Ethan Instagram  0.037700
149     Ashante Instagram  1.300000
150     Heather Instagram  0.064500
151      Johney Instagram  0.002818
152       Falon Instagram  0.236000
153       Kylie Instagram  0.003012
154      Alicia Instagram  0.006570
155     Xochitl Instagram  0.002851
156      Jaasir Instagram  0.039800
157       Amber Instagram  0.000000
158     Brandon Instagram  0.122000
159        Rana Instagram  0.015900
160     Jameela Instagram  0.000459
161     Breanna Instagram  0.114000
162      Victor Instagram  0.001928
163     Mckenna Instagram  0.019200
164     Matthew Instagram  0.036600
165     Chelsea Instagram  0.000000
166       Blake Instagram  0.002878
167    Jordynne Instagram  0.001471
168       Sarah Instagram  0.056500
169     Desmond Instagram  0.006116
170   Alexandra Instagram  0.249000
171     Mahboob Instagram  0.084500
172        Xiao Instagram  0.019300
173       Julio Instagram  0.001462
174       Numa Instagram  0.000480
175      Daniel Instagram  0.054900
176      Keisha Instagram  0.038800
177   Shaquille Instagram  0.001876
178        Kara Instagram  0.001439
179      Lauren Instagram  0.149000
180       Kevin Instagram  0.000000
181        Gene  Facebook  0.007575
182     Yoonjoo  Facebook  1.100000
183       Bruce  Facebook  0.001200
184      Lauryn  Facebook  0.004528
185         Eli  Facebook  0.009964
186    Danielle  Facebook  0.009003
187     Makayla  Facebook  0.021634
188       Jacky  Facebook  0.000292
189       Kelly  Facebook  0.000000
190      Olivia  Facebook  0.156000
191        Sahl  Facebook  0.078983
192        Kiel  Facebook  0.017000
193   Shantiana  Facebook  0.233374
194       Belle  Facebook  0.161573
195      Jeremy  Facebook  0.014684
196      Jaasir  Facebook  0.001494
197     Zuhaira  Facebook  0.009570
198      Khaira  Facebook  0.028940
199     Chelsea  Facebook  3.100000
200     Viviana  Facebook  0.182149
201    Muzammil  Facebook  0.007312
202    Gabriela  Facebook  0.214692
203      Isaiah  Facebook  0.002075
204   Anjanetta  Facebook  0.002177
205     Dimitri  Facebook  0.001302
206   Abbigelle  Facebook  0.000000
207       Hinna  Facebook  0.003285
208       Ethan  Facebook  0.028915
209     Ashante  Facebook  1.958553
210     Heather  Facebook  0.100000
211      Johney  Facebook  0.004370
212       Falon  Facebook  2.900000
213       Kylie  Facebook  0.037525
214      Alicia  Facebook  0.002348
215     Xochitl  Facebook  0.006795
216      Jaasir  Facebook  0.043135
217       Amber  Facebook  0.038273
218     Brandon  Facebook  0.054236
219        Rana  Facebook  0.001504
220     Jameela  Facebook  0.000000
221     Breanna  Facebook  0.018074
222      Victor  Facebook  0.078139
223     Mckenna  Facebook  0.000000
224     Matthew  Facebook  0.004010
225     Chelsea  Facebook  0.000000
226       Blake  Facebook  0.016021
227    Jordynne  Facebook  0.003817
228       Sarah  Facebook  0.017835
229     Desmond  Facebook  0.001974
230   Alexandra  Facebook  0.098445
231     Mahboob  Facebook  0.011106
232        Xiao  Facebook  0.012203
233       Julio  Facebook  0.010359
234       Numa  Facebook  0.000000
235      Daniel  Facebook  0.008060
236      Keisha  Facebook  1.700276
237   Shaquille  Facebook  0.001203
238        Kara  Facebook  0.000000
239      Lauren  Facebook  0.000000
240       Kevin  Facebook  0.049425

CodePudding user response:

It is slightly unclear if this is what you were talking about especially as you have no labels on your y axis, but I think your problem is that you are ordering your x values within the aes call. So therefore your y values are already set but you are effectively assigning them a random x value as they were out of order.

You are better off to assign a level to the influencers based on the total number of followers to order your plot

library(dplyr)
#count total followers and arrange plot 
bars <- bars %>% group_by(influencers) %>% 
  mutate(total =sum(followers)) %>% arrange(total)

#set influencers levels based on total followers
bars <- bars %>% mutate(influencers = factor(influencers, levels = unique(bars$influencers)))

#plot without reordering
ggplot(bars, 
       aes(fill=networks, y=followers, x=influencers))   
  geom_bar(position="stack", stat="identity") 
  coord_flip() 
  ylab("Followers (millions)")  
  xlab("")  
  theme_bw()`

CodePudding user response:

Soo... I'm kind of embarrassed right now. I've misunderstood the graph, I realized just now that they're, as the function says, stacked! I thought it was juxtaposed.

I'm sorry for the trouble and many thanks to those who helped me!

  • Related