Bar on side of plot that shows second group/category











up vote
0
down vote

favorite












I created a plot that visualizes data as I intended with this code:



library(ggplot2)

data = data.frame()

for (x in seq(1,10)){
data = rbind(data, data.frame(x = rnorm(10, sd=0.1) + x, y = seq(10,1), group = x,
category = c(rep("a", 3), rep("b", 3), rep("c", 3), "d")))
}

data$group = as.factor(data$group)

ggplot() +
theme(legend.position="none", panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title.y = element_blank(), axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data = data, mapping = aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data = data, mapping = aes(x=x, y=y, col= group))


enter image description here



I now want to add a bar at the side that explains the grouping defined in the category column. A legend or labels of this category should also be shown.
enter image description here










share|improve this question






















  • How are colors and height mapped to categories in the bar?
    – PoGibas
    Nov 22 at 12:14












  • The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
    – Benni
    Nov 22 at 12:20










  • @Benni Have you seen my answer? Does it help?
    – jay.sf
    Nov 22 at 18:40















up vote
0
down vote

favorite












I created a plot that visualizes data as I intended with this code:



library(ggplot2)

data = data.frame()

for (x in seq(1,10)){
data = rbind(data, data.frame(x = rnorm(10, sd=0.1) + x, y = seq(10,1), group = x,
category = c(rep("a", 3), rep("b", 3), rep("c", 3), "d")))
}

data$group = as.factor(data$group)

ggplot() +
theme(legend.position="none", panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title.y = element_blank(), axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data = data, mapping = aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data = data, mapping = aes(x=x, y=y, col= group))


enter image description here



I now want to add a bar at the side that explains the grouping defined in the category column. A legend or labels of this category should also be shown.
enter image description here










share|improve this question






















  • How are colors and height mapped to categories in the bar?
    – PoGibas
    Nov 22 at 12:14












  • The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
    – Benni
    Nov 22 at 12:20










  • @Benni Have you seen my answer? Does it help?
    – jay.sf
    Nov 22 at 18:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I created a plot that visualizes data as I intended with this code:



library(ggplot2)

data = data.frame()

for (x in seq(1,10)){
data = rbind(data, data.frame(x = rnorm(10, sd=0.1) + x, y = seq(10,1), group = x,
category = c(rep("a", 3), rep("b", 3), rep("c", 3), "d")))
}

data$group = as.factor(data$group)

ggplot() +
theme(legend.position="none", panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title.y = element_blank(), axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data = data, mapping = aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data = data, mapping = aes(x=x, y=y, col= group))


enter image description here



I now want to add a bar at the side that explains the grouping defined in the category column. A legend or labels of this category should also be shown.
enter image description here










share|improve this question













I created a plot that visualizes data as I intended with this code:



library(ggplot2)

data = data.frame()

for (x in seq(1,10)){
data = rbind(data, data.frame(x = rnorm(10, sd=0.1) + x, y = seq(10,1), group = x,
category = c(rep("a", 3), rep("b", 3), rep("c", 3), "d")))
}

data$group = as.factor(data$group)

ggplot() +
theme(legend.position="none", panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title.y = element_blank(), axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data = data, mapping = aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data = data, mapping = aes(x=x, y=y, col= group))


enter image description here



I now want to add a bar at the side that explains the grouping defined in the category column. A legend or labels of this category should also be shown.
enter image description here







r ggplot2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 11:53









Benni

1752211




1752211












  • How are colors and height mapped to categories in the bar?
    – PoGibas
    Nov 22 at 12:14












  • The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
    – Benni
    Nov 22 at 12:20










  • @Benni Have you seen my answer? Does it help?
    – jay.sf
    Nov 22 at 18:40


















  • How are colors and height mapped to categories in the bar?
    – PoGibas
    Nov 22 at 12:14












  • The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
    – Benni
    Nov 22 at 12:20










  • @Benni Have you seen my answer? Does it help?
    – jay.sf
    Nov 22 at 18:40
















How are colors and height mapped to categories in the bar?
– PoGibas
Nov 22 at 12:14






How are colors and height mapped to categories in the bar?
– PoGibas
Nov 22 at 12:14














The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
– Benni
Nov 22 at 12:20




The colors of the bar should be different from the plot (the similarity is by accident). The exact height is not important, as long as you can see the mapping of the "rows" to the bar
– Benni
Nov 22 at 12:20












@Benni Have you seen my answer? Does it help?
– jay.sf
Nov 22 at 18:40




@Benni Have you seen my answer? Does it help?
– jay.sf
Nov 22 at 18:40












1 Answer
1






active

oldest

votes

















up vote
2
down vote













First generate a category vector



# list the categories by their freqencies
cgy <- as.data.frame(table(data$category), stringsAsFactors=FALSE)
# convert the values so that a "0" category finds place
# the "0" category is needed because groups starting at "1"
cgy$Freq <- cgy$Freq/10*9
cgy <- rbind(c(0, 10), cgy) # bind a "0" category
# get the desired vector
cgy.v <- unlist(sapply(1:nrow(cgy), function(x) rep(cgy[x, 1], cgy[x, 2])))
rm(cgy) # clean up


Your plot



p <- ggplot() +
theme(legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data=data, mapping=aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data=data, mapping=aes(x=x, y=y, col= group))


Add



p + geom_bar(data=data, aes(0, y = as.integer(category)/22),
fill = factor(sort(cgy.v),
labels=(c("white", "blue", "orange", "yellow", "green"))),
stat = "identity") +
# the labels:
geom_text(aes(x=0,
y=c(as.integer(table(cgy.v)))/10,
label=c("",sort(unique(cgy.v))[-1])),
size=4,
color="black", position=position_stack(vjust=.5))


Yielding



enter image description here






share|improve this answer























  • The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
    – Benni
    Nov 23 at 13:57










  • You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
    – jay.sf
    Nov 23 at 17:48











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%2f53430457%2fbar-on-side-of-plot-that-shows-second-group-category%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













First generate a category vector



# list the categories by their freqencies
cgy <- as.data.frame(table(data$category), stringsAsFactors=FALSE)
# convert the values so that a "0" category finds place
# the "0" category is needed because groups starting at "1"
cgy$Freq <- cgy$Freq/10*9
cgy <- rbind(c(0, 10), cgy) # bind a "0" category
# get the desired vector
cgy.v <- unlist(sapply(1:nrow(cgy), function(x) rep(cgy[x, 1], cgy[x, 2])))
rm(cgy) # clean up


Your plot



p <- ggplot() +
theme(legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data=data, mapping=aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data=data, mapping=aes(x=x, y=y, col= group))


Add



p + geom_bar(data=data, aes(0, y = as.integer(category)/22),
fill = factor(sort(cgy.v),
labels=(c("white", "blue", "orange", "yellow", "green"))),
stat = "identity") +
# the labels:
geom_text(aes(x=0,
y=c(as.integer(table(cgy.v)))/10,
label=c("",sort(unique(cgy.v))[-1])),
size=4,
color="black", position=position_stack(vjust=.5))


Yielding



enter image description here






share|improve this answer























  • The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
    – Benni
    Nov 23 at 13:57










  • You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
    – jay.sf
    Nov 23 at 17:48















up vote
2
down vote













First generate a category vector



# list the categories by their freqencies
cgy <- as.data.frame(table(data$category), stringsAsFactors=FALSE)
# convert the values so that a "0" category finds place
# the "0" category is needed because groups starting at "1"
cgy$Freq <- cgy$Freq/10*9
cgy <- rbind(c(0, 10), cgy) # bind a "0" category
# get the desired vector
cgy.v <- unlist(sapply(1:nrow(cgy), function(x) rep(cgy[x, 1], cgy[x, 2])))
rm(cgy) # clean up


Your plot



p <- ggplot() +
theme(legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data=data, mapping=aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data=data, mapping=aes(x=x, y=y, col= group))


Add



p + geom_bar(data=data, aes(0, y = as.integer(category)/22),
fill = factor(sort(cgy.v),
labels=(c("white", "blue", "orange", "yellow", "green"))),
stat = "identity") +
# the labels:
geom_text(aes(x=0,
y=c(as.integer(table(cgy.v)))/10,
label=c("",sort(unique(cgy.v))[-1])),
size=4,
color="black", position=position_stack(vjust=.5))


Yielding



enter image description here






share|improve this answer























  • The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
    – Benni
    Nov 23 at 13:57










  • You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
    – jay.sf
    Nov 23 at 17:48













up vote
2
down vote










up vote
2
down vote









First generate a category vector



# list the categories by their freqencies
cgy <- as.data.frame(table(data$category), stringsAsFactors=FALSE)
# convert the values so that a "0" category finds place
# the "0" category is needed because groups starting at "1"
cgy$Freq <- cgy$Freq/10*9
cgy <- rbind(c(0, 10), cgy) # bind a "0" category
# get the desired vector
cgy.v <- unlist(sapply(1:nrow(cgy), function(x) rep(cgy[x, 1], cgy[x, 2])))
rm(cgy) # clean up


Your plot



p <- ggplot() +
theme(legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data=data, mapping=aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data=data, mapping=aes(x=x, y=y, col= group))


Add



p + geom_bar(data=data, aes(0, y = as.integer(category)/22),
fill = factor(sort(cgy.v),
labels=(c("white", "blue", "orange", "yellow", "green"))),
stat = "identity") +
# the labels:
geom_text(aes(x=0,
y=c(as.integer(table(cgy.v)))/10,
label=c("",sort(unique(cgy.v))[-1])),
size=4,
color="black", position=position_stack(vjust=.5))


Yielding



enter image description here






share|improve this answer














First generate a category vector



# list the categories by their freqencies
cgy <- as.data.frame(table(data$category), stringsAsFactors=FALSE)
# convert the values so that a "0" category finds place
# the "0" category is needed because groups starting at "1"
cgy$Freq <- cgy$Freq/10*9
cgy <- rbind(c(0, 10), cgy) # bind a "0" category
# get the desired vector
cgy.v <- unlist(sapply(1:nrow(cgy), function(x) rep(cgy[x, 1], cgy[x, 2])))
rm(cgy) # clean up


Your plot



p <- ggplot() +
theme(legend.position="none",
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()) +
geom_path(data=data, mapping=aes(x=x, y=y, col= group, alpha=0.5)) +
geom_point(data=data, mapping=aes(x=x, y=y, col= group))


Add



p + geom_bar(data=data, aes(0, y = as.integer(category)/22),
fill = factor(sort(cgy.v),
labels=(c("white", "blue", "orange", "yellow", "green"))),
stat = "identity") +
# the labels:
geom_text(aes(x=0,
y=c(as.integer(table(cgy.v)))/10,
label=c("",sort(unique(cgy.v))[-1])),
size=4,
color="black", position=position_stack(vjust=.5))


Yielding



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 17:58

























answered Nov 22 at 13:46









jay.sf

4,27721437




4,27721437












  • The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
    – Benni
    Nov 23 at 13:57










  • You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
    – jay.sf
    Nov 23 at 17:48


















  • The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
    – Benni
    Nov 23 at 13:57










  • You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
    – jay.sf
    Nov 23 at 17:48
















The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
– Benni
Nov 23 at 13:57




The scale is a bit problematic. How can this approach be modified to autoscale the bars depending on the number of rows? The bar should also start and stop with the rows. And is there also the possibility to add a label or legend to the bars?
– Benni
Nov 23 at 13:57












You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
– jay.sf
Nov 23 at 17:48




You were right, didn't look pretty. I found this tricky, but I can provide a "hack". Also there are labels now. See my edited answer. What do you think, might this help you?
– jay.sf
Nov 23 at 17:48


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430457%2fbar-on-side-of-plot-that-shows-second-group-category%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo