I am trying to recreate this solution using ggplot2 in R: Combining two stacked bar plots for a grouped stacked bar plot
diamonds %>%
filter(color=="D"|color=="E"|color=="F") %>%
mutate(dummy=rep(c("a","b"),each=13057)) %>%
ggplot(aes(x=color,y=price))+
geom_bar(aes(fill=clarity),stat="identity",width=.25)+
facet_wrap(~cut)
I added a new variable to the diamonds dataset called dummy. dummy has two values: a and b. Let's say I want to compare these two values by creating a bar graph that has two stacked bars right next to each other (one for each value of dummy) for each value of color. How can I manipulate this such that there are two stacked bars for each value of color?
I think it would involve position dodge and/or a separate legend, but I've been unsuccessful so far. I do not want to add another facet - I want these both on the x-axis within each facet.
Similiar to the approach in the post you have linked one option to achieve your desired result would be via two geom_col and by converting the x axis variable to a numeric like so. However, doing so requires to set the breaks and labels manually via scale_x_continuous. Additionally I made use of the ggnewscale package to add a second fill scale:
library(ggplot2)
library(dplyr)
d <- diamonds %>%
filter(color == "D" | color == "E" | color == "F") %>%
mutate(dummy = rep(c("a", "b"), each = 13057))
ggplot(mapping = aes(y = price)) +
geom_col(data = filter(d, dummy == "a"), aes(x = as.numeric(color) - .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "a", guide = guide_legend(order = 1)) +
scale_x_continuous(breaks = seq_along(levels(d$color)), labels = levels(d$color)) +
ggnewscale::new_scale_fill() +
geom_col(data = filter(d, dummy == "b"), aes(x = as.numeric(color) + .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "b", option = "B", guide = guide_legend(order = 2)) +
facet_wrap(~cut)
Related
I want to present two groups with their development over time in a boxplot, and add labels of significance for each group (intra-group comparisons). I have a working example using ggpubr::stat_compare_means, but I am unable to correctly position the two geoms.
I have attempted position = position_dodge(width=0.5)and several other positional elements, but for some reasons they wont move at all. My desired output would have each label set horizontally shifted to be over each groups box, and vertically adjusted to not overlap.
Example code using diamonds:
df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
geom_boxplot() +
stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))
Although I really don't think this is a good idea for visualisation - here is a solution. If you use ggpubr, stay in the ggpubr syntax. And use faceting for subgrouping.
P.S. Try a table instead.
library(tidyverse)
library(ggpubr)
mydf <- filter(diamonds, color == "J" | color == "E")
comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))
ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
stat_compare_means(
method = "t.test", ref.group = "Fair", label = "p.format",
comparisons = comparisons
)
Created on 2020-03-20 by the reprex package (v0.3.0)
I am currently working in a comparison between two inventory levels and I want to plot two step graphs in the same grid with a color code. This is my code.
Intento1<-data.frame(Fecha, NivelI)
Intento2<-data.frame(Fecha, Nivel2)
#Printing the step graphs in one grid
ggplot()+geom_step(Intento1, mapping=aes(x=Fecha, y=NivelI))+geom_step(Intento2, mapping=aes(x=Fecha, y=Nivel2))
And it works fine plotting both graphs in the same grid, I could also add a different color to each graph but I couldnĀ“t add the little colored labels that appear normally at the right. All support is appreciated.
For example data dummy,
dummy <- data.table(
Fecha = seq(as.Date("2020/1/1"), as.Date("2020/1/31"), "day")
)
dummy$NivelI = runif(31, 0, 10)
dummy$Nivel2 = runif(31, 0, 10)
plot using reshape2::melt like below will work.
dummy %>%
melt(id.vars = "Fecha") %>%
ggplot(aes(Fecha, value, group = variable, color = variable)) +
geom_step() + guides(color = guide_legend(title = "aaa"))
In your case, to make dummy formed data, if Fecha, NivelI and Nivel2 are vectors, just try
df <- data.frame(
Fecha,
NivelI,
Nivel2
)
then
df %>%
melt(id.vars = "Fecha") %>%
ggplot(aes(Fecha, value, group = variable, color = variable)) +
geom_step() + guides(color = guide_legend(title = "aaa"))
where "aaa" will be your legend name.
I want to present two groups with their development over time in a boxplot, and add labels of significance for each group (intra-group comparisons). I have a working example using ggpubr::stat_compare_means, but I am unable to correctly position the two geoms.
I have attempted position = position_dodge(width=0.5)and several other positional elements, but for some reasons they wont move at all. My desired output would have each label set horizontally shifted to be over each groups box, and vertically adjusted to not overlap.
Example code using diamonds:
df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
geom_boxplot() +
stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))
Although I really don't think this is a good idea for visualisation - here is a solution. If you use ggpubr, stay in the ggpubr syntax. And use faceting for subgrouping.
P.S. Try a table instead.
library(tidyverse)
library(ggpubr)
mydf <- filter(diamonds, color == "J" | color == "E")
comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))
ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
stat_compare_means(
method = "t.test", ref.group = "Fair", label = "p.format",
comparisons = comparisons
)
Created on 2020-03-20 by the reprex package (v0.3.0)
I map the same variable (color) to color in two different geoms. I want them either to appear in separate legends (DHJ and EFI) or preferably just skip the second legend (for E, F, and I) altogether. Currently, R mixes the two together and gives me a legend that lists DEFHIJ in alphabetical order all mixed together.
Basically, I want to graph today's points onto some smoothed lines that use a standard dataset. I don't want there to be a legend for the smoothed lines - we are all familiar with them and they are standard on all our graphs. I just want a legend for the points only.
I've tried show.legend = FALSE as suggested elsewhere, but that doesn't seem to have an effect. guides(color = FALSE) removes the entire legend.
Reprex:
library(tidyverse)
set1 <- diamonds %>%
filter(color %in% c("D", "H", "J"))
set2 <- diamonds %>%
filter(color %in% c("E", "F", "I"))
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
show.legend = FALSE,
aes(x = x, y = y, color = color))
Here is the graph that is produced. It has all 6 letters in the legend, instead of only DHJ.
If you want the legend to show only the colors from one dataset you can do so by setting the breaks in scale_color_discrete() to those values.
... +
scale_color_discrete(breaks = unique(set1$color) )
If you aren't using the colors of the lines, since this is standard background info, you could add the lines by using group ingeom_smooth() instead of color. (Also see linetype if you wanted to be able to tell the lines apart.)
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
aes(x = x, y = y, group = color))
This question already has answers here:
pie chart with ggplot2 with specific order and percentage annotations
(2 answers)
Closed 5 years ago.
I'm trying to add some percent labels to a pie chart but any of the solutions works. The thing is that the chart displays the number of tasks completed grouped by category.
output$plot2<-renderPlot({
ggplot(data=data[data$status=='100% completed',], aes(x=factor(1), fill=category))+
geom_bar(width = 1)+
coord_polar("y")
Using geom_text with position_stack to adjust the label locations would work.
library(ggplot2)
library(dplyr)
# Create a data frame which is able to replicate your plot
plot_frame <- data.frame(category = c("A", "B", "B", "C"))
# Get counts of categories
plot_frame <- plot_frame %>%
group_by(category) %>%
summarise(counts = n()) %>%
mutate(percentages = counts/sum(counts)*100)
# Plot
ggplot(plot_frame, aes(x = factor(1), y = counts)) +
geom_col(aes(fill = category), width = 1) +
geom_text(aes(label = percentages), position = position_stack(vjust = 0.5)) +
coord_polar("y")
The codes above generate this:
You might want to change the y-axis from counts to percentages since you are labeling the latter. In that case, change the values passed to ggplot accordingly.