Add Legend and change Colour on grouped bar plot - r

I created a plot like this;
library("ggplot2")
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = color, y = ..prop.., group = 2)) +
scale_y_continuous(labels=scales::percent) +
facet_grid(~cut)
Now I want to add a legend for the variable "color", also I want to change the colour of the bars. The graph is exactly how I want it to be, and if possible I don't want change the structure of the dataset, just add a legend and change colours.
I could not find example that fit for this "percentage"-style graphics.

ggplot(data = diamonds, aes(x = color, y = ..prop.., group = cut)) +
geom_bar(aes(fill = factor(..x.., labels = LETTERS[seq(from = 4, to = 10 )]))) +
labs(fill = "color") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ cut)

Related

ggplot: adding a frequency plot over a percentage plot

I am interested in doing a plot showing percentages by group.
something like this:
data(iris)
ggplot(iris,
aes(x = Sepal.Length, group = factor(Species), fill = factor(Species))) +
geom_histogram(position = "fill")+theme_bw()
however, I would also like to plot a histogram showing the frequency distribution on top of this graph.
something like the plot below.
ggplot(iris,aes(x = Sepal.Length)) +
geom_histogram()+theme_bw()
Does anyone know how to do this?
Note I know how to do a frequency plot by group: ggplot(iris,aes(x = Sepal.Length, group = factor(Species), fill = factor(Species))) + geom_histogram()+theme_bw(). But this is not what I want. Rather I would like a small frequency distribution at the bottom of the percentage plot presented at the beginning.
Thank you very much
Something like this?
library(gridExtra)
p1 <- ggplot(iris,
aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species))) +
geom_histogram(position = "fill") +
theme_bw() +
theme(legend.position = "top")
p2 <- ggplot(iris,aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species))) +
geom_histogram() +
theme_bw() +
theme(legend.position = "none")
grid.arrange(p1, p2,
heights = c(4, 1.5))
Edit: So you are looking for this then? Note that in this case the absolute values of the smaller histogram become meaningless since they were scaled down to be ~25% of the vertical chart range.
ggplot() +
geom_histogram(data = iris,
aes(x = Sepal.Length,
group = factor(Species),
fill = factor(Species)),
position = "fill",
alpha = 1) +
geom_histogram(data = iris,
aes(x = Sepal.Length,
y = ..ncount.. / 4),
alpha = 0.5,
fill = 'black')

ggplot2 Squashes Labels and Data When Using: scale_x_discrete(limit = c())

So I have this graph:
But I would like to add appropriate labels to the x axis so each column has a label. For this I used scale_x_discrete(limit = as.character(2008:2017)) however doing so gives this result:
As you can see the labels are all squashed to one side and the data bars to the other.
Am I doing something wrong? or is this bug?
Here is my code:
# First image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none")
# Second image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none") +
scale_x_discrete(limit = as.character(2008:2017))
Your labeling problem is due to the variable type and it has not nothing to do with the axis scale. Try changing the variable, in the x axis, from numerical to categorical:
data_melt$year <- as.factor(data_melt$year)

Create proportion bar plot with dodge position

I am trying to create the following plot but with proportion on the y axis.
library(ggplot2)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
but when I add y=..prop.., it doesn't group it by clarity. I have tried the following:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., fill = clarity), position = "dodge")
To calculate proportion (or frequency) you can use ..count.. (proportion is specific count divided by all count's):
library(ggplot2)
ggplot(diamonds, aes(cut, (..count..) / sum(..count..), fill = clarity)) +
geom_bar(position = "dodge")

How to add colour and legend in multiple line plot on ggplot without using melt from reshape package?

I am plotting two lines on same plot with sme x axis by following lines.
i am implementing the lower line but unable to see colors and legend
ggplot(final, aes(x = Date)) + geom_line(aes(y = cocastock)) + geom_line(aes(y = procterstock)) + scale_color_manual(values = c(cocastock = '#008B00', procterstock = '#FFFFFF'))
also tried
ggplot(final, aes(x = Date)) + geom_line(aes(y = cocastock)) + geom_line(aes(y = procterstock)) + scale_color_manual(values = c('#008B00','#FFFFFF'))
but dosen't work
scale_colour_manual only works when you have specified colour in aes, hence you need:
ggplot(final, aes(x = Date)) +
geom_line(aes(y = cocastock, colour = "cocastock")) +
geom_line(aes(y = procterstock, colour = "procterstock")) +
scale_color_manual(values = c(cocastock = '#008B00', procterstock = '#FFFFFF'))

ggplot2 - annotate means in several graph

I try to mark my graphs with the average specific of each graph :
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0,2.5) +
geom_text(data = NULL, x = 0.6, y = 0, label = mean(carat), size=5) +
coord_flip()
For example, here I would like the graph of "Fair" is displayed average of "Fair", that of "Good" is displayed average of "Good", etc.
Also, but this is an extra, I would like to be positioned with respect to x if the average is 1.0, while the average is displayed at x = 1.0
There are a number of ways to get the labels (and the positions for the labels). Here, the dplyr package is used to summarise the diamonds data frame; that is, to obtain the required means. Also note that the labels are formatted - two decimal places. In the code below, the diamonds2 data frame contains the means and the labels, and is used in the call to geom_text.
library(ggplot2)
library(dplyr)
diamonds2 = transform(summarise(group_by(diamonds, cut), label = mean(carat)),
Label = sprintf("%.02f", label))
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0, 2.5) +
geom_text(data = diamonds2, aes(label = Label, x = label, y = 0), size=5) +
coord_flip()

Resources