How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?

Rlearner

I have the attached datasets and use this R code to plot the data:

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)

The resulting plot is attached as well.

Now I need to add numbers from different datasets on top of each bar. For example:

  1. on top of "Inside Firewall (Dest)" should be the numbers from sampleNumIFdest.csv
  2. on top of "Inside Firewall (Source)" should be the numbers from sampleNumIFsource.csv
  3. on top of "Outside Firewall (Dest)" should be the numbers from sampleNumOFdest.csv
  4. on top of "Outside Firewall (Source)" should be the numbers from sampleNumOFsource.csv

I have tried to use geom_text() but I do not know how to read the numbers from the different datasets. Please note, that the datasets have different number of rows (which causes additional problems for me). Any suggestion is highly appreciated.

The attached files are here.

Sorry, I had to zip all my files as I am not allowed to add more then 2 URLs in my post.

Jaap

I think the best solution is to combine all the datasets into one:

# 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

When you have done that, you can use geom_text to plot the numbers on top of the bars:

# 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"))

The result: enter image description here

As you can see, the text labels overlap sometimes. You can change that by decreasing the size of the text, but then you run the risk that the labels become hard to read. You might therefore consider to use facets by adding facet_grid(type ~ .) (or facet_wrap(~ type)) to the plotting code:

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 ~ .)

which results in the following plot:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add labels into the bars in a bar graph using ggplot2 in R?

R geom_bar and facet_grid labels on top of bars

How to set custom labels on the bars?

Add Data Labels On Top of Bar in a Barplot in R

How to add percentage labels in a stacked bar keeping bars heights (R plotly)

How to add frequency count labels to the bars in a bar graph using ggplot2?

Centering labels at the bars using ggplot2 and moving labels to the top of the errorbar in R

How to add count labels to the right of the bars in a horizontal bar chart?

How to add unique values on top of bars that is not the value of the bar?

How to add/remove custom labels from the axis

LWC Chart js: How to display dataset labels on each of the bars below in single horizontal stacked bar chart js

How to add data labels at the top of crossbars using ggplot2

How to add new values in dataset using R

How to add "grouped" labels using ggplot in R?

How to add percentages on top of bars in seaborn?

How to add bar labels using Matplotlib

How to add SE bars to bar-graph in R code?

How to add percentage labels in proper positions on ggplot bar plot with geom_text when using facet wrap and remove empty space between bars?

How to add custom vertex labels to a ggnet graph in R?

Add Count Labels on Top of Barchart in base R

How To Create Pyramid Bar Chart in R with y-axis labels between the bars

How to hide/show bars differentiate by a colors and by click on labels, using chartjs bar charts?

Add percentage labels to a stacked bar chart above bars

How to shift bars from y-axis using barplot() R

How to add labels to a bar chart by numbering. (Making histogram from a list using turtle in python 3.7+)

How can I create a horizontal stacked bar chart with labels on the bars themselves as well as labels above the bars?

How to add labels on top of polygons in leaflet

How to add top and bottom labels for dot graph?

How to split dataset to images and labels using tensorflow