How to plot a boxplot with correctly spaced continuous x-axis values in ggplot2
up vote
1
down vote
favorite
I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:
library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()
This gives me the plot
where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.
r ggplot2
add a comment |
up vote
1
down vote
favorite
I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:
library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()
This gives me the plot
where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.
r ggplot2
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:
library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()
This gives me the plot
where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.
r ggplot2
I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:
library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()
This gives me the plot
where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.
r ggplot2
r ggplot2
edited Nov 11 at 2:20
asked Nov 11 at 2:06
SinghD
154
154
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividingx
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
– SinghD
Nov 11 at 2:54
you can use thewidth=
argument to geom_boxplot. Set the width in x units
– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividingx
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
– SinghD
Nov 11 at 2:54
you can use thewidth=
argument to geom_boxplot. Set the width in x units
– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
add a comment |
up vote
1
down vote
accepted
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividingx
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
– SinghD
Nov 11 at 2:54
you can use thewidth=
argument to geom_boxplot. Set the width in x units
– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
edited Nov 11 at 15:58
answered Nov 11 at 2:21
dww
13.6k22551
13.6k22551
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividingx
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
– SinghD
Nov 11 at 2:54
you can use thewidth=
argument to geom_boxplot. Set the width in x units
– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
add a comment |
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividingx
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
– SinghD
Nov 11 at 2:54
you can use thewidth=
argument to geom_boxplot. Set the width in x units
– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing
x
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?– SinghD
Nov 11 at 2:54
This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing
x
by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?– SinghD
Nov 11 at 2:54
you can use the
width=
argument to geom_boxplot. Set the width in x units– dww
Nov 11 at 3:44
you can use the
width=
argument to geom_boxplot. Set the width in x units– dww
Nov 11 at 3:44
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
– Tjebo
Nov 11 at 11:50
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
@Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
– dww
Nov 11 at 16:00
add a comment |
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%2f53245233%2fhow-to-plot-a-boxplot-with-correctly-spaced-continuous-x-axis-values-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