R 数据帧操作
我有以下数据框:R 数据帧操作,r,ggplot2,plyr,R,Ggplot2,Plyr,我有以下数据框: head(d,20) place total error value 348 Telecolumbus_GmbH 2 2 2 349 telefonica 5 2
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
我想做一个基本的条形图,其中place
位于x轴上,y轴根据每个位置的总和计算%(值/总数)
我使用了以下内容,但它没有给出正确的%值,因为我无法找到正确的组合%,比如位置“dtag”
您应该首先使用
plyr
包总结数据帧,然后创建条形图:
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))
require(plyr)
需要(ggplot2)
#将您的数据帧总结为一个新的数据帧
d2
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))
# 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))