How can I add a line below axis labels just like in the attached picture where there is a line below 50,100 and 200 axis labels?
Asked
Active
Viewed 1,142 times
2

M.R.Wani
- 107
- 1
- 11
-
https://stackoverflow.com/questions/12409960/ggplot2-annotate-outside-of-plot is one way to go, but using `segmentsGrob` & `textGrob`s but these things are a bit more esily done in base r as in meW answer – user20650 Jan 02 '19 at 11:53
2 Answers
1
An example (doesn't contain GGPLOT2):
data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
at=c(0, 5), #Limit of line
col="red",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks
Multiple lines, just append another axis
command as -
data("mtcars")
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
axis(1, # Put 1 for X-axis, 2 for Y-axis
at=c(0, 2.5), #Limit of line
col="red",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks
axis(1, # Put 1 for X-axis, 2 for Y-axis
at=3+c(0, 2.5), #Limit of line
col="blue",
line=2.5, # how much gap you need between line and X-axis
labels=rep("",2), # remove line labels
lwd=2,
lwd.ticks=0) # remove ticks

meW
- 3,832
- 7
- 27
-
I get this error: Error in axis(1, at = c(0, 5), col = "red", line = 2.5, labels = rep("", : plot.new has not been called yet – M.R.Wani Jan 02 '19 at 10:40
-
you need to call the plot command, which means plot the figure first then draw the line – meW Jan 02 '19 at 10:41
-
If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE ) – M.R.Wani Jan 02 '19 at 10:44
-
-
If following is my data frame, how can I add the line? conc <- c("0","50","100","200") values <- c(40.83,2.16,8.16,18.66,30.16) df <- data.frame(conc,values) ggplot(df, aes(x = conc,y = values, fill = conc))+ geom_bar(stat = "identity",show.legend = FALSE ) – M.R.Wani Jan 02 '19 at 10:51
-
you have error while forming dataframe. Provide equal number of values in both columns and also provide limit of line. – meW Jan 02 '19 at 10:53
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186043/discussion-between-m-r-wani-and-mew). – M.R.Wani Jan 02 '19 at 11:34
-
df <- data.frame(conc = c(0,50,100,150), values = c(2,8,18,30)) df$conc <- as.factor(df$conc) df %>% ggplot(aes(x=conc,y=values,fill = conc))+geom_bar(stat = "identity",show.legend = FALSE) How to add a line below x-axis labels from 0 to 100 and write "conc in mg" below that line – M.R.Wani Jan 02 '19 at 11:53
-
for ggplot2 refer: https://stackoverflow.com/questions/51848658/ggplot-adding-tracking-colors-below-x-axis – meW Jan 02 '19 at 11:55
0
Maybe the facet_wrap
from ggplot2
is a starting point:
library(magrittr)
library(ggplot2)
mtcars %>%
dplyr::mutate(hp_200 = ifelse(hp > 200, "hp > 200", "hp <= 200")) %>%
ggplot(aes(x = hp)) +
geom_histogram(binwidth = 20) +
facet_wrap(~ hp_200, scales = "free_x", strip.position = "bottom") +
ggthemes::theme_hc()
Created on 2019-01-02 by the reprex package (v0.2.1)

Birger
- 1,111
- 7
- 17