I can´t plot a geom_bar correctly - r

I'm trying to improve my skills on R language and I found a problem.
#Load the library.
library(ggplot2)
#Execute a simple code
ggplot(mtcars, aes(x = cyl, fill = am)) + geom_bar()
My main question is, what I'm doing bad, why the fill aesthetic has not been plotted

Adrian. In the way that you are using it, with geom_bar(), fill should be a factor rather than a continuous variable.
ggplot(mtcars, aes(x = cyl, fill = as.character(am))) ## as.character or as.vector transform "am"
+ geom_bar()
To ilustate the differenece in ggplot's behavior between vector and numeric, look at this plot:
ggplot(mtcars, aes(x = cyl, fill = as.character(am), color = as.character(am), alpha = am))
+ geom_bar()

Related

Subdividing jitter points in ggplot

I'm having difficulty getting geom_jitter to separate my data points by a third variable (phase).
I'd like for each bar in this 2x2 graph to have its own set of jittered data points for each phase condition.
Right now the points are grouped between the two phase bars (sorry couldn't upload the image -- don't have my 10 posts yet).
I know I'm missing something in the geom_jitter aes, but not sure what it is -- fill=phase is not doing the trick.
ggplot(datalong, aes(grp, score)) +
geom_bar(stat = "summary", fun.y = "mean", aes(fill=phase), position = "dodge", alpha = .5) +
geom_jitter(inherit.aes = FALSE, aes(grp, score, fill=phase), position = position_jitter(0.2))
The default shape for points does not take the fill aesthetic
Compare
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, color = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter(shape = 21)
Created on 2020-07-09 by the reprex package (v0.3.0)
Found a solution using:
geom_point(position=position_jitterdodge())
This seems to jitter and dodge to separate the points.

Change fill/colour for geom_dotplot or geom_histogram with a continuous variable

Is it possible to fill ggplot's geom_dotplot with continuous variables?
library(ggplot2)
ggplot(mtcars, aes(x = mpg, fill = disp)) +
geom_dotplot()
this should be pretty straightforward, but I've tried messing with the groups aes and no success.
The max I can do is to discretize the disp variable but it is not optimal.
ggplot(mtcars, aes(x = mpg, fill = factor(disp))) +
geom_dotplot()
Good question! You have to set group = variable within aes (where variable is equal to the same column that you're using for fill or color):
library(ggplot2)
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_dotplot()
geom_dotplot in away is just like a histogram. You can't set fill/colour there easily as grouping is done. To make it work you have to set group.
Example using geom_histogram:
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_histogram()

ggplot geom_bar() fill not coloring bars on plot

Using the fill argument on geom_bar is not coloring the bars on my plot. I'm using the train.csv from the titanic data set here.
passengers <- read.csv('../input/train.csv')
I have tried moving the fill outside of the aes(), tried moving the aes up to the ggplot() function.
This is the code I'm using on the Titanic Data set
ggplot(data = passengers) +
geom_bar(mapping = aes(x=Survived, fill = Pclass))
This is the code I'm using as a template which works fine on the ggplot built in diamonds data.
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut))
I just keep getting grey bars with the geom_bar for Survived using Pclass as the fill.
This is doing the trick for me:
ggplot(data = passengers) +
geom_bar(mapping = aes(x=Survived, fill = as.character(Pclass)))
You can try as.factor()
ggplot(data = passengers) +
geom_bar(mapping = aes(x=Survived, fill = as.factor(passengers$Pclass)))
Probably your variables is not factor

ggplot boxplot: same colour looks different depending when it is set

Just encountered a surprising colour problem while doing a boxplot with ggplot2.
The same colour (#FF4040) looks drastically different whether I set it as fill parameter or later in scale_fill_manual.
Here is an example you can copy/paste using the mtcars dataset.
library(ggplot2)
data('mtcars')
ggplot (data = mtcars, aes(x = as.factor(cyl), disp)) +
geom_boxplot(aes(fill = '#FF4040'))
ggplot (data = mtcars, aes(x = as.factor(cyl), disp)) +
geom_boxplot(aes(fill = as.factor(cyl)))+
scale_fill_manual(breaks=c('4', '6', '8'),
values=c('#FF4040', '#FF4040', '#FF4040'))
Here is the comparison:
As I said in comments in first example you don't change fill color only mapping fill. So instead of geom_boxplot(aes(fill= '#FF4040')) put geom_boxplot(fill= '#FF4040') and you recive the same result as the second version.

How would you plot a box plot and specific points on the same plot?

We can draw box plot as below:
qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot")
and point as:
qplot(factor(cyl), mpg, data = mtcars, geom = "point")
How would you combine both - but just to show a few specific points(say when wt is less than 2) on top of the box?
If you are trying to plot two geoms with two different datasets (boxplot for mtcars, points for a data.frame of literal values), this is a way to do it that makes your intent clear. This works with the current (Sep 2016) version of ggplot (ggplot2_2.1.0)
library(ggplot2)
ggplot() +
# box plot of mtcars (mpg vs cyl)
geom_boxplot(data = mtcars,
aes(x = factor(cyl), y= mpg)) +
# points of data.frame literal
geom_point(data = data.frame(x = factor(c(4,6,8)), y = c(15,20,25)),
aes(x=x, y=y),
color = 'red')
I threw in a color = 'red' for the set of points, so it's easy to distinguish them from the points generated as part of geom_boxplot
Use + geom_point(...) on your qplot (just add a + geom_point() to get all the points plotted).
To plot selectively just select those points that you want to plot:
n <- nrow(mtcars)
# plot every second point
idx <- seq(1,n,by=2)
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(cyl)[idx],y=mpg[idx]) ) # <-- see [idx] ?
If you know the points before-hand, you can feed them in directly e.g.:
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(c(4,6,8)),y=c(15,20,25)) ) # plot (4,15),(6,20),...
You can show both by using ggplot() rather than qplot(). The syntax may be a little harder to understand, but you can usually get much more done. If you want to plot both the box plot and the points you can write:
boxpt <- ggplot(data = mtcars, aes(factor(cyl), mpg))
boxpt + geom_boxplot(aes(factor(cyl), mpg)) + geom_point(aes(factor(cyl), mpg))
I don't know what you mean by only plotting specific points on top of the box, but if you want a cheap (and probably not very smart) way of just showing points above the edge of the box, here it is:
boxpt + geom_boxplot(aes(factor(cyl), mpg)) + geom_point(data = ddply(mtcars, .(cyl),summarise, mpg = mpg[mpg > quantile(mpg, 0.75)]), aes(factor(cyl), mpg))
Basically it's the same thing except for the data supplied to geom_point is adjusted to include only the mpg numbers in the top quarter of the distribution by cylinder. In general I'm not sure this is good practice because I think people expect to see points beyond the whiskers only, but there you go.

Resources