How can I add a line below axis labels in ggplot2?
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?
r
add a comment |
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?
r
stackoverflow.com/questions/12409960/… is one way to go, but usingsegmentsGrob
&textGrob
s but these things are a bit more esily done in base r as in meW answer
– user20650
Jan 2 at 11:53
add a comment |
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?
r
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?
r
r
asked Jan 2 at 10:15
M.R.WaniM.R.Wani
406
406
stackoverflow.com/questions/12409960/… is one way to go, but usingsegmentsGrob
&textGrob
s but these things are a bit more esily done in base r as in meW answer
– user20650
Jan 2 at 11:53
add a comment |
stackoverflow.com/questions/12409960/… is one way to go, but usingsegmentsGrob
&textGrob
s but these things are a bit more esily done in base r as in meW answer
– user20650
Jan 2 at 11:53
stackoverflow.com/questions/12409960/… 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 2 at 11:53
stackoverflow.com/questions/12409960/… 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 2 at 11:53
add a comment |
2 Answers
2
active
oldest
votes
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
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
|
show 4 more comments
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)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004510%2fhow-can-i-add-a-line-below-axis-labels-in-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
|
show 4 more comments
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
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
|
show 4 more comments
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
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
edited Jan 2 at 11:19
answered Jan 2 at 10:27
meWmeW
2,745119
2,745119
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
|
show 4 more comments
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
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 2 at 10:40
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 2 at 10:40
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 at 10:41
you need to call the plot command, which means plot the figure first then draw the line
– meW
Jan 2 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 2 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 2 at 10:44
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
What do you mean by these variables? Elaborate.
– meW
Jan 2 at 10:45
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 2 at 10:51
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 2 at 10:51
|
show 4 more comments
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)
add a comment |
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)
add a comment |
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)
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)
answered Jan 2 at 17:10
BirgerBirger
655112
655112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004510%2fhow-can-i-add-a-line-below-axis-labels-in-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/12409960/… 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 2 at 11:53