我有附加的数据集并使用此R代码绘制数据:
plotData <- read.csv("plotdata.csv")
ix <- 1:nrow(plotData)
long <- melt(transform(plotData, id = ix), id = "id") # add id col; melt to long form
ggp2 <- ggplot(long, aes(id, value, fill = variable))+geom_bar(stat = "identity", position = "dodge")+
scale_x_continuous(breaks = ix) +
labs(y='Throughput (Mbps)',x='Nodes') +
scale_fill_discrete(name="Legend",
labels=c("Inside Firewall (Dest)",
"Inside Firewall (Source)",
"Outside Firewall (Dest)",
"Outside Firewall (Source)")) +
theme(legend.position="right") + # The position of the legend
theme(legend.title = element_text(colour="blue", size=14, face="bold")) + # Title appearance
theme(legend.text = element_text(colour="blue", size = 12, face = "bold")) # Label appearance
plot(ggp2)
也会附上结果图。
现在我需要在每个条形图的顶部添加来自不同数据集的数字。例如:
我尝试使用geom_text()
,但我不知道如何从不同的数据集中读取数字。请注意,数据集具有不同的行数(这会给我带来额外的问题)。任何建议都受到高度赞赏。
抱歉,我必须压缩所有文件,因为我不允许在帖子中添加超过2个网址。
答案 0 :(得分:4)
我认为最好的解决方案是将所有数据集合并为一个:
# loading the different datasets
plotData <- read.csv("plotData.csv")
IFdest <- read.table("sampleNumIFdest.csv", sep="\t", header=TRUE, strip.white=TRUE)
IFsource <- read.table("sampleNumIFsource.csv", sep="\t", header=TRUE, strip.white=TRUE)
OFdest <- read.table("sampleNumOFdest.csv", sep="\t", header=TRUE, strip.white=TRUE)
OFsource <- read.table("sampleNumOFsource.csv", sep="\t", header=TRUE, strip.white=TRUE)
# add an id
ix <- 1:nrow(plotData)
plotData$id <- 1:nrow(plotData)
plotData <- plotData[,c(5,1,2,3,4)]
# combine the different dataframe
plotData$IFdest <- c(IFdest$Freq, NA)
plotData$IFsource <- c(IFsource$Freq, NA, NA)
plotData$OFdest <- OFdest$Freq
plotData$OFsource <- c(OFsource$Freq, NA, NA)
# reshape the dataframe
long <- cbind(
melt(plotData, id = c("id"), measure = c(2:5),
variable = "type", value.name = "value"),
melt(plotData, id = c("id"), measure = c(6:9),
variable = "name", value.name = "numbers")
)
long <- long[,-c(4,5)] # this removes two unneceassary columns
完成后,您可以使用geom_text
在数字顶部绘制数字:
# create your plot
ggplot(long, aes(x = id, y = value, fill = type)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = numbers), vjust=-1, position = position_dodge(0.9), size = 3) +
scale_x_continuous(breaks = ix) +
labs(x = "Nodes", y = "Throughput (Mbps)") +
scale_fill_discrete(name="Legend",
labels=c("Inside Firewall (Dest)",
"Inside Firewall (Source)",
"Outside Firewall (Dest)",
"Outside Firewall (Source)")) +
theme_bw() +
theme(legend.position="right") +
theme(legend.title = element_text(colour="blue", size=14, face="bold")) +
theme(legend.text = element_text(colour="blue", size=12, face="bold"))
结果:
如您所见,文本标签有时会重叠。您可以通过减小文本的大小来改变它,但随后又冒着标签变得难以阅读的风险。因此,您可以考虑通过在绘图代码中添加facet_grid(type ~ .)
(或facet_wrap(~ type)
)来使用构面:
ggplot(long, aes(x = id, y = value, fill = type)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = numbers), vjust=-0.5, position = position_dodge(0.9), size = 3) +
scale_x_continuous("Nodes", breaks = ix) +
scale_y_continuous("Throughput (Mbps)", limits = c(0,1000)) +
scale_fill_discrete(name="Legend",
labels=c("Inside Firewall (Dest)",
"Inside Firewall (Source)",
"Outside Firewall (Dest)",
"Outside Firewall (Source)")) +
theme_bw() +
theme(legend.position="right") +
theme(legend.title = element_text(colour="blue", size=14, face="bold")) +
theme(legend.text = element_text(colour="blue", size=12, face="bold")) +
facet_grid(type ~ .)
导致以下情节: