p <- data %>%
ggplot(aes(x=rating)) +
geom_histogram( binwidth=10, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
ggtitle("Distribution of teams fifa ratings") +
theme_ipsum() + theme(plot.title = element_text(size=15))
#I am trying to plot this histogram but I don't know why the plot is not shown
#This code unlikely contains mistakes since I have copied It from https://www.r-graph-gallery.com/220-basic-ggplot2-histogram.html#binSize
Your binwidth argument is the culprit. You can confirm by removing that and seeing if it works.
Set a binwidth that is compatible with the values for rating. This should solve the issue.
Related
Before reading any longer, I suggest you to download and see the original codes in this question posted in this forum.
I run this ggplot code:
ggplot(data=data, aes(x=X2, y=Count, group=X3, colour=X3)) +
geom_point(size=5) +
geom_line() +
xlab("Decils") +
ylab("% difference in nº Pk") +
ylim(-50,25) + ggtitle("CL") +
geom_hline(aes(yintercept=0), lwd=1, lty=2) +
scale_x_discrete(limits=c(orden_deciles)) +
coord_polar() +
geom_area(aes(color=X3, fill=X3), alpha=0.2) +
And got this plot:
As you may imagine, something's wrong with the code. Blue group seems to be colored properly. I would like to color all the groups, taking as reference the black-slashed ring, which represent 0.
How can I implement this?
geom_area() has a default position argument of stack. This stack each area over the others, creating weird results in this case.
Simply change the position to identity:
library(ggplot2)
## This was not given, I supposed it's in this form
orden_deciles <- paste0('DC', 1:10)
ggplot(data=data, aes(x=X2, y=Count, group=X3, colour=X3)) +
geom_point(size=5) +
geom_line() +
xlab("Decils") +
ylab("% difference in nº Pk") +
ylim(-50,25) + ggtitle("CL") +
geom_hline(aes(yintercept=0), lwd=1, lty=2) +
scale_x_discrete(limits=c(orden_deciles)) +
geom_area(aes(fill=X3), alpha=0.2, position = position_identity()) +
coord_polar()
#> Warning: Removed 5 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_path).
Created on 2018-05-15 by the reprex package (v0.2.0).
I think this is a fairly simple question, I just can't figure it out for the life of me.
I am making a boxplot using the following code;
ggplot(data, aes(gear, length)) + geom_boxplot() + xlab('Gear Type') +
ylab('Size (cm)') + ggtitle("Catch Characterization") +
theme(plot.title = element_text(hjust = 0.5))
This produces an aggregated boxplot for my entire dataset, I would like to be able to produce the same boxplot for two subsets of my dataset. Specifically I have another column "action" with either the character "D" or "K". For example MySQL mind wants to just add in a WHERE clause (however I know that is not how it works) such as;
ggplot(data, aes(gear, length, WHEREaction=D)) + geom_boxplot() + xlab('Gear Type') +
ylab('Size (cm)') + ggtitle("Catch Characterization") +
theme(plot.title = element_text(hjust = 0.5))
Edit, I am able to use facet_wrap to parse out and graph based on "action" however I am curious how I could just make one graph where of (length, gear) where "action = D". I know I can fairly easily just restructure my data just wondering if there is a simpler/quicker way to do so with the aggregated data set?
Edit again, think I figured it out by piecing a few things together, I ended up using this code and it seems to be giving me the appropriate graphs;
ggplot(aggdata[aggdata$action == "D",], aes(gear, length)) +
geom_boxplot() + xlab('Gear') + ylab('Size (cm)') +
ggtitle("Discard Characterization") +
theme(plot.title = element_text(hjust = 0.5))
I'm running in a bit of a problem plotting some data with ggplot2: I want to use a facet_wrap over a variable AdultInputProp, but R doesn't find the variable and instead returns an Error in as.quoted(facets) : object 'AdultInputProp' not found. Now I understand that this simply means that R can't find this variable in the dataset used to plot, but if I ask ggplot2 to instead use the same variable for to create a shape scale, it works just fine. Any idea what the problem might be?
Sorry, I'm not too sure how to make a minimal working example with a generated df from scratch, so here's the df I'm using, and the code bellow. I've also tried using facet_grid instead of facet_wrap but ran into the same problem.
The code here with facets returns the above-mentioned error:
df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
aes(x=TestIteration, y=Error,
colour=GoalBabblingProp,
group=interaction(GoalBabblingProp,
AdultInputProp))) +
facet_wrap(AdultInputProp) +
xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
scale_colour_discrete(name = "Goal babbling proportion") +
geom_line(position = position_dodge(1000)) +
geom_errorbar(aes(ymin=Error-ci,
ymax=Error+ci),
color="black", width=1000,
position = position_dodge(1000)) +
geom_point(position = position_dodge(1000),
size=1.5, fill="white")
This other code, exactly the same except for the facet_wrap line deleted and with shape added works fine:
df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
aes(x=TestIteration, y=Error,
colour=GoalBabblingProp,
shape=AdultInputProp,
group=interaction(GoalBabblingProp,
AdultInputProp))) +
xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
scale_colour_discrete(name = "Goal babbling proportion") +
geom_line(position = position_dodge(1000)) +
geom_errorbar(aes(ymin=Error-ci,
ymax=Error+ci),
color="black", width=1000,
position = position_dodge(1000)) +
geom_point(position = position_dodge(1000),
size=1.5, fill="white")
facet_wrap expects a formula, not just a naked variable name. So you should change it to
...
facet_wrap(~ AdultInputProp) +
...
I am trying to create a graph on R but keep getting this error...
Error in inherits(mapping, "uneval") : object 'EI.TOT' not found
I have put in this command:
hist.1 <- ggplot(data, EI.TOT) + theme(legend.position = "none") + geom_histogram(aes(y=..density..), colour="black", fill="white") + labs(x="EI.TOT", y = "Density")
EI.TOT is the header of one of my columns. I have run a regression using these values but when I want to create a histogram is says that this column is not found.
Your trouble is that you dont specify your aes correctly. Try this code, let me know if it does not work on your data.
library(ggplot2)
data <- data.frame(EI.TOT =rnorm(60))
hist.1 <- ggplot(data=data) +
theme(legend.position = "none") +
geom_histogram(aes(EI.TOT),colour="black", fill="white", binwidth = 0.5) +
labs(x="EI.TOT", y = "Density")
hist.1
Change bindwidth to change the breaks in the plot.
I am trying to plot the outliers and mean point for the box plots in below using the data available here. The dataset has 3 different factors and 1 value column for 3600 rows.
While I run the below the code it shows the mean point but doesn't draw the outliers properly
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
Again, while I am modify the code like in below the mean points disappear !!
ggplot(df, aes(x=Representations, y=Values, colour=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
In both of the cases I am getting the message: "ymax not defined: adjusting position using y instead" 3 times.
Any kind suggestions how to fix it? I would like to draw the mean points within individual box plots and show outliers in the same colour as the plots.
EDIT:
The original data set does not have any outliers and that was reason for my confusion. Thanks to MrFlick's answer with randomly generated data which clarifies it properly.
Rather than downloading the data, I just made a random sample.
set.seed(18)
gg <- expand.grid (
Methods=c("BC","FD","FDFND","NC"),
Metrics=c("DM","DTI","LB"),
Representations=c("CHG","QR","HQR")
)
df <- data.frame(
gg,
Values=rnorm(nrow(gg)*50)
)
Then you should be able to create the plot you want with
library(ggplot2)
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
stat_summary(fun.y="mean", geom="point",
position=position_dodge(width=0.75), color="white") +
facet_wrap(~Metrics)
which gave me
I was using ggplot2 version 0.9.3.1