Frage

I have the following data frame:

head(d,20)

                                        place        total      error       value
348                         Telecolumbus_GmbH          2          2           2
349                                telefonica          5          2           2
350                 SOCO_SoftCom_Datensysteme          1          2           2
351                          SWU_TeleNet_GmbH          1          2           2
352                                      dtag          5          2           2
353                                      dtag         23          2          14
354 Fachhochschule_Braunschweig/Wolfenbuettel          1          2           2
355       Unitymedia_dynamic_customer_IP_pool          3          2           2
356                                   EWE-TEL          3          2           2
357               QSC_AG_Dynamic_IP_Addresses          4          2           2
358                                telefonica          1          2           2
359                                telefonica          1          2           2
360                                      dtag          2          2           2
361                      Northern_Access_GmbH          2          2           2
362            WT-CMTS-PPPOE-PRIVATE-CUSTOMER          2          2           2
363                                      dtag         17          2           2
364                                DHCP_Space          5          2           2
365   Kabel_Deutschland_Breitband_Customer_14          3          2           2
366                                      dtag          5          2           2
367   Kabel_Deutschland_Breitband_Customer_20          6          2           2

I want to make a basic bar plot where place is on the x-axis and y-axis has the %(value/total) factored according to the sum of each place.

I used the following, but it does not gives the proper % value as I am unable to find the proper combined % of say place "dtag"

ggplot(data = d,aes(x = factor(place),y = value/total)) + geom_bar(stat='identity') + theme(axis.text.x = element_text(angle = 90, hjust = 0.5))
War es hilfreich?

Lösung

You should first summarise your dataframe with the plyr package and then create a barplot:

require(plyr)
require(ggplot2)

# summarise your dataframe into a new one
d2 <- ddply(d, .(place), summarise,
             tot = sum(total),
             err = sum(error),
             val = sum(value))

# create the plot
ggplot(data = d2, aes(x = place, y = val/tot)) + 
  geom_bar(stat='identity') + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5))

As an alternative you can do it like this:

# create a new variable first
d2$ratio <- d2$val / d2$tot

# create the plot
ggplot(data = d2, aes(x = place, y = ratio)) + 
  geom_bar(stat='identity') + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5))

The result from the first approach (looks the same for both plotting solutions): enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top