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
enter image description here



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



enter image description here



How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?










share|improve this question




















  • 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 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










  • @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















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
enter image description here



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



enter image description here



How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?










share|improve this question




















  • 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 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










  • @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













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
enter image description here



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



enter image description here



How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?










share|improve this question















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
enter image description here



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



enter image description here



How can I adjust y-axis on first plot, ie show percentage instead count? or remove those zeroes in the second?







r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 12:52

























asked Nov 21 at 12:41









rmorelli74

364




364








  • 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 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










  • @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














  • 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 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










  • @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








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

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)