stacked bar ggplot in log scale
I'm trying to do a stacked barplot with ggplot in R,using log_10 scale for the y-axis, but I'm getting the wrong plot.
This is my code:
library(reshape2)
library(ggplot2)
DF <- structure(list(Worker = 1:18, Seed = c(300, 40, 200, 0.1, 0.1,
0.1, 1000, 0.1, 0.1, 30, 45, 0.1, 13, 0.1, 50, 0.1, 0.1, 30),
Boosted = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
35, 0.1, 0.1, 13, 0.1, 0.1, 0.1, 0.1, 0.1), Online = c(0.1,
0.1, 0.1, 15000, 60, 500, 0.1, 100, 450, 0.1, 200, 500, 0.1,
5000, 0.1, 70, 1000, 500)), class = "data.frame", row.names = c(NA,
-18L))
DF1 <- melt(DF, id.var="Worker")
ggplot(DF1, aes(x = Worker, y = value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("id") +
ylab("# of x") +
labs(fill = "") +
scale_fill_manual(values = cols,
labels = c("Mobile", "Laptop","Online User")) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)) +
expand_limits(x = 1, y = 1) +
scale_y_log10(limits = c(1,1e5),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)))
Below you see the image where the bars for some ids are wrong: see for instance ids 10 and 13.
What am I missing? I also tried just using log(value)
in ggplot(DF1, aes(x = Worker, y = log(value), fill = variable))
, but I'm getting the same.
r ggplot2 bar-chart
add a comment |
I'm trying to do a stacked barplot with ggplot in R,using log_10 scale for the y-axis, but I'm getting the wrong plot.
This is my code:
library(reshape2)
library(ggplot2)
DF <- structure(list(Worker = 1:18, Seed = c(300, 40, 200, 0.1, 0.1,
0.1, 1000, 0.1, 0.1, 30, 45, 0.1, 13, 0.1, 50, 0.1, 0.1, 30),
Boosted = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
35, 0.1, 0.1, 13, 0.1, 0.1, 0.1, 0.1, 0.1), Online = c(0.1,
0.1, 0.1, 15000, 60, 500, 0.1, 100, 450, 0.1, 200, 500, 0.1,
5000, 0.1, 70, 1000, 500)), class = "data.frame", row.names = c(NA,
-18L))
DF1 <- melt(DF, id.var="Worker")
ggplot(DF1, aes(x = Worker, y = value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("id") +
ylab("# of x") +
labs(fill = "") +
scale_fill_manual(values = cols,
labels = c("Mobile", "Laptop","Online User")) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)) +
expand_limits(x = 1, y = 1) +
scale_y_log10(limits = c(1,1e5),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)))
Below you see the image where the bars for some ids are wrong: see for instance ids 10 and 13.
What am I missing? I also tried just using log(value)
in ggplot(DF1, aes(x = Worker, y = log(value), fill = variable))
, but I'm getting the same.
r ggplot2 bar-chart
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
6
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
5
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31
add a comment |
I'm trying to do a stacked barplot with ggplot in R,using log_10 scale for the y-axis, but I'm getting the wrong plot.
This is my code:
library(reshape2)
library(ggplot2)
DF <- structure(list(Worker = 1:18, Seed = c(300, 40, 200, 0.1, 0.1,
0.1, 1000, 0.1, 0.1, 30, 45, 0.1, 13, 0.1, 50, 0.1, 0.1, 30),
Boosted = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
35, 0.1, 0.1, 13, 0.1, 0.1, 0.1, 0.1, 0.1), Online = c(0.1,
0.1, 0.1, 15000, 60, 500, 0.1, 100, 450, 0.1, 200, 500, 0.1,
5000, 0.1, 70, 1000, 500)), class = "data.frame", row.names = c(NA,
-18L))
DF1 <- melt(DF, id.var="Worker")
ggplot(DF1, aes(x = Worker, y = value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("id") +
ylab("# of x") +
labs(fill = "") +
scale_fill_manual(values = cols,
labels = c("Mobile", "Laptop","Online User")) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)) +
expand_limits(x = 1, y = 1) +
scale_y_log10(limits = c(1,1e5),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)))
Below you see the image where the bars for some ids are wrong: see for instance ids 10 and 13.
What am I missing? I also tried just using log(value)
in ggplot(DF1, aes(x = Worker, y = log(value), fill = variable))
, but I'm getting the same.
r ggplot2 bar-chart
I'm trying to do a stacked barplot with ggplot in R,using log_10 scale for the y-axis, but I'm getting the wrong plot.
This is my code:
library(reshape2)
library(ggplot2)
DF <- structure(list(Worker = 1:18, Seed = c(300, 40, 200, 0.1, 0.1,
0.1, 1000, 0.1, 0.1, 30, 45, 0.1, 13, 0.1, 50, 0.1, 0.1, 30),
Boosted = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
35, 0.1, 0.1, 13, 0.1, 0.1, 0.1, 0.1, 0.1), Online = c(0.1,
0.1, 0.1, 15000, 60, 500, 0.1, 100, 450, 0.1, 200, 500, 0.1,
5000, 0.1, 70, 1000, 500)), class = "data.frame", row.names = c(NA,
-18L))
DF1 <- melt(DF, id.var="Worker")
ggplot(DF1, aes(x = Worker, y = value, fill = variable)) +
geom_bar(stat = "identity") +
xlab("id") +
ylab("# of x") +
labs(fill = "") +
scale_fill_manual(values = cols,
labels = c("Mobile", "Laptop","Online User")) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)) +
expand_limits(x = 1, y = 1) +
scale_y_log10(limits = c(1,1e5),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)))
Below you see the image where the bars for some ids are wrong: see for instance ids 10 and 13.
What am I missing? I also tried just using log(value)
in ggplot(DF1, aes(x = Worker, y = log(value), fill = variable))
, but I'm getting the same.
r ggplot2 bar-chart
r ggplot2 bar-chart
edited Jan 19 at 0:44
Nestorghh
asked Jan 18 at 23:58
NestorghhNestorghh
1,51841729
1,51841729
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
6
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
5
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31
add a comment |
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
6
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
5
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
6
6
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
5
5
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31
add a comment |
0
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',
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%2f54262890%2fstacked-bar-ggplot-in-log-scale%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54262890%2fstacked-bar-ggplot-in-log-scale%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
A couple of lines of code seem to be missing from your example.
– Julius Vainora
Jan 19 at 0:05
Yes, let me update it . Basically, have to reshape it
– Nestorghh
Jan 19 at 0:07
6
If I may editorialize, stacked bar charts are a confusing match for a log axis, as not only does the height not scale in proportion to the magnitude, but the ordering of the categories will change the interpretation of the same sized bar. For instance, does a bar from 10^0 to 10^1 represents 9 (10 - 1), or 10? Does a bar the same height from 10^1 to 10^2 represent 90?
– Jon Spring
Jan 19 at 0:22
5
log scales and bar charts are almost always a terrible combination (height does not map to quantity). A stacked bar is even worse. Best advice is to find a different way to present your data.
– dww
Jan 19 at 0:31