如何使用R中的ggplot / geom_bar在数据集顶部添加自定义标签?

时间:2014-04-01 05:05:35

标签: r ggplot2 geom-bar

我有附加的数据集并使用此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)

也会附上结果图。

现在我需要在每个条形图的顶部添加来自不同数据集的数字。例如:

  1. 位于&#34;内部防火墙(Dest)&#34;应该是sampleNumIFdest.csv
  2. 中的数字
  3. 位于&#34;内部防火墙(来源)&#34;应该是sampleNumIFsource.csv
  4. 中的数字
  5. 位于&#34;外部防火墙(Dest)&#34;应该是sampleNumOFdest.csv
  6. 中的数字
  7. 在&#34;防火墙外(源)&#34;应该是sampleNumOFsource.csv
  8. 中的数字

    我尝试使用geom_text(),但我不知道如何从不同的数据集中读取数字。请注意,数据集具有不同的行数(这会给我带来额外的问题)。任何建议都受到高度赞赏。

    The attached files are here

    抱歉,我必须压缩所有文件,因为我不允许在帖子中添加超过2个网址。

1 个答案:

答案 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"))

结果: enter image description here

如您所见,文本标签有时会重叠。您可以通过减小文本的大小来改变它,但随后又冒着标签变得难以阅读的风险。因此,您可以考虑通过在绘图代码中添加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 ~ .)

导致以下情节:

enter image description here