r - 如何在 R 中使用 ggplot/geom_bar 从数据集中添加自定义标签?

标签 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. 在“Inside Firewall (Dest)”的顶部应该是来自 sampleNumIFdest.csv 的数字
  2. 在“Inside Firewall (Source)”的顶部应该是来自 sampleNumIFsource.csv 的数字
  3. 在“Outside Firewall (Dest)”的顶部应该是 sampleNumOFdest.csv 中的数字
  4. 在“Outside Firewall (Source)”的顶部应该是来自 sampleNumOFsource.csv 的数字

我曾尝试使用 geom_text() 但我不知道如何从不同的数据集中读取数字。请注意,数据集的行数不同(这给我带来了额外的问题)。非常感谢任何建议。

The attached files are here .

抱歉,我不得不压缩我所有的文件,因为我不允许在我的帖子中添加超过 2 个 URL。

最佳答案

我认为最好的解决方案是将所有数据集合并为一个:

# 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))添加到绘图代码来使用 facets:

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

关于r - 如何在 R 中使用 ggplot/geom_bar 从数据集中添加自定义标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22777245/

相关文章:

python - 如何在 R 或 Python 中制作 3d(4 变量)三元(金字塔)图?

r - 如何在R中绘制条形图的次轴?

r - ggplot2:如果位置 = "fill",则在条形图上添加标签

r - 带有聚合数据的堆叠条形图 (ggplot2)

r - 使用 geom_tile( ) 在 ggplot2 中绘制序列索引图

r - 自动将 p 值添加到 facet plot

r - 按 2 列有效分组占第三列的百分比

r - 使用 kableExtra 将分组变量与剥离/阴影匹配

html - 如何在 Shiny 中更改导航栏页面折叠的断点

r - 加载 R 包时如何分配变量可用?