Adjust geom_histogram labels on bars VS correct scale on y-axis
up vote
0
down vote
favorite
I have this plot
ggplot(data, aes(D3)) +
geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) +
geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) +
scale_fill_gradient("", low = "green", high = "red")
which outputs a this nice histogram
as you can see y axis shows count instead of percentage.
On other hand if I switch to
ggplot(data, aes(D3)) +
geom_histogram(aes(y = (..density..)), binwidth = 1) +
geom_text(aes( label = format(100*..density.., digits=1,), y= ..density.. ), stat= "bin", binwidth =1, vjust = -0.2 ) +
scale_y_continuous(labels = scales::percent)
it outputs this (quite ugly) plot with zeroes on the y axis
How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?
r ggplot2
add a comment |
up vote
0
down vote
favorite
I have this plot
ggplot(data, aes(D3)) +
geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) +
geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) +
scale_fill_gradient("", low = "green", high = "red")
which outputs a this nice histogram
as you can see y axis shows count instead of percentage.
On other hand if I switch to
ggplot(data, aes(D3)) +
geom_histogram(aes(y = (..density..)), binwidth = 1) +
geom_text(aes( label = format(100*..density.., digits=1,), y= ..density.. ), stat= "bin", binwidth =1, vjust = -0.2 ) +
scale_y_continuous(labels = scales::percent)
it outputs this (quite ugly) plot with zeroes on the y axis
How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?
r ggplot2
1
This is easy enough to help with, using somedplyr
or other functions, if you make a reproducible example
– camille
Nov 21 at 13:19
@camille well, I know how to deal with this usingdplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using..count..
without usingdplyr
.
– rmorelli74
Nov 21 at 13:45
It's a little hard to tell without an example dataset, but my guess is you wanty = stat(count/sum(count))
insideaes()
in bothgeom_histogram()
andgeom_text()
. Then you can addscale_y_continuous(labels = scales::percent)
. (Thestat()
code was introduced as the replacement for the..
names starting in ggplot2 version 3.0.0.)
– aosmith
Nov 21 at 19:48
@aosmithdt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this plot
ggplot(data, aes(D3)) +
geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) +
geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) +
scale_fill_gradient("", low = "green", high = "red")
which outputs a this nice histogram
as you can see y axis shows count instead of percentage.
On other hand if I switch to
ggplot(data, aes(D3)) +
geom_histogram(aes(y = (..density..)), binwidth = 1) +
geom_text(aes( label = format(100*..density.., digits=1,), y= ..density.. ), stat= "bin", binwidth =1, vjust = -0.2 ) +
scale_y_continuous(labels = scales::percent)
it outputs this (quite ugly) plot with zeroes on the y axis
How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?
r ggplot2
I have this plot
ggplot(data, aes(D3)) +
geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) +
geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) +
scale_fill_gradient("", low = "green", high = "red")
which outputs a this nice histogram
as you can see y axis shows count instead of percentage.
On other hand if I switch to
ggplot(data, aes(D3)) +
geom_histogram(aes(y = (..density..)), binwidth = 1) +
geom_text(aes( label = format(100*..density.., digits=1,), y= ..density.. ), stat= "bin", binwidth =1, vjust = -0.2 ) +
scale_y_continuous(labels = scales::percent)
it outputs this (quite ugly) plot with zeroes on the y axis
How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?
r ggplot2
r ggplot2
edited Nov 21 at 12:52
asked Nov 21 at 12:41
rmorelli74
364
364
1
This is easy enough to help with, using somedplyr
or other functions, if you make a reproducible example
– camille
Nov 21 at 13:19
@camille well, I know how to deal with this usingdplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using..count..
without usingdplyr
.
– rmorelli74
Nov 21 at 13:45
It's a little hard to tell without an example dataset, but my guess is you wanty = stat(count/sum(count))
insideaes()
in bothgeom_histogram()
andgeom_text()
. Then you can addscale_y_continuous(labels = scales::percent)
. (Thestat()
code was introduced as the replacement for the..
names starting in ggplot2 version 3.0.0.)
– aosmith
Nov 21 at 19:48
@aosmithdt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago
add a comment |
1
This is easy enough to help with, using somedplyr
or other functions, if you make a reproducible example
– camille
Nov 21 at 13:19
@camille well, I know how to deal with this usingdplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using..count..
without usingdplyr
.
– rmorelli74
Nov 21 at 13:45
It's a little hard to tell without an example dataset, but my guess is you wanty = stat(count/sum(count))
insideaes()
in bothgeom_histogram()
andgeom_text()
. Then you can addscale_y_continuous(labels = scales::percent)
. (Thestat()
code was introduced as the replacement for the..
names starting in ggplot2 version 3.0.0.)
– aosmith
Nov 21 at 19:48
@aosmithdt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago
1
1
This is easy enough to help with, using some
dplyr
or other functions, if you make a reproducible example– camille
Nov 21 at 13:19
This is easy enough to help with, using some
dplyr
or other functions, if you make a reproducible example– camille
Nov 21 at 13:19
@camille well, I know how to deal with this using
dplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using ..count..
without using dplyr
.– rmorelli74
Nov 21 at 13:45
@camille well, I know how to deal with this using
dplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using ..count..
without using dplyr
.– rmorelli74
Nov 21 at 13:45
It's a little hard to tell without an example dataset, but my guess is you want
y = stat(count/sum(count))
inside aes()
in both geom_histogram()
and geom_text()
. Then you can add scale_y_continuous(labels = scales::percent)
. (The stat()
code was introduced as the replacement for the ..
names starting in ggplot2 version 3.0.0.)– aosmith
Nov 21 at 19:48
It's a little hard to tell without an example dataset, but my guess is you want
y = stat(count/sum(count))
inside aes()
in both geom_histogram()
and geom_text()
. Then you can add scale_y_continuous(labels = scales::percent)
. (The stat()
code was introduced as the replacement for the ..
names starting in ggplot2 version 3.0.0.)– aosmith
Nov 21 at 19:48
@aosmith
dt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
@aosmith
dt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53412265%2fadjust-geom-histogram-labels-on-bars-vs-correct-scale-on-y-axis%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
1
This is easy enough to help with, using some
dplyr
or other functions, if you make a reproducible example– camille
Nov 21 at 13:19
@camille well, I know how to deal with this using
dplyr
and calculating percentage before going through ggplot but I am working with young students (17yr) and would like to keep it simple as possible using..count..
without usingdplyr
.– rmorelli74
Nov 21 at 13:45
It's a little hard to tell without an example dataset, but my guess is you want
y = stat(count/sum(count))
insideaes()
in bothgeom_histogram()
andgeom_text()
. Then you can addscale_y_continuous(labels = scales::percent)
. (Thestat()
code was introduced as the replacement for the..
names starting in ggplot2 version 3.0.0.)– aosmith
Nov 21 at 19:48
@aosmith
dt <- as.data.frame(rnorm(500, 2000))
colnames(dt) <- "D3"
dt$D3 <- as.integer(dt$D3)
ggplot(dt, aes(D3)) + geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) + geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) + scale_fill_gradient("", low = "green", high = "red")
– rmorelli74
Nov 22 at 7:26
You can go ahead and edit your question to add your dataset (don't forget to set your seed so it is reproducible!). Did you try what I suggested? I'm pretty sure it does what you want now that I see your example dataset.
– aosmith
2 days ago