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))
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.
r ggplot2
add a comment |
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))
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.
r ggplot2
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
add a comment |
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))
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.
r ggplot2
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))
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.
r ggplot2
r ggplot2
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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%2f53430457%2fbar-on-side-of-plot-that-shows-second-group-category%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
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