This is a continuation of (how to modify axis labels ggplot in R). The code supplied works beautifully. The data is created manually. So I transferred that a larger dataset and am now getting the following error.
Error in aes(y = AnnualDifference, x = (reorder(Seriesttls, AnnualDifference))) + :
non-numeric argument to binary operator
This is the code being used
jobgrowthbyindustry<-ggplot(data=nvces2, aes(y=AnnualDifference,x=
(reorder(Seriesttls,AnnualDifference)))+geom_col(color="blue")
+coord_flip()+labs(x=NULL)+ggtitle("Nevada Nonfarm Job Growth by Industry"))+
theme(plot.title.position = "plot",
plot.title = element_text(hjust =0.5))
If this is of any assistance, the AnnualDifference item is created using the following code
nvces<- nvces%>%
group_by(adjusted,areaname,seriescode)%>%
mutate(PreviousYear=lag(ravg,12),
PreviousMonth=lag(ravg),
AnnualDifference=ravg-PreviousYear,
MonthlyDifference=ravg-PreviousMonth,
MonthlyGrowthRate=MonthlyDifference/PreviousMonth,
PercentGrowthRate=AnnualDifference/PreviousYear)%>%
ungroup()
Where my confusion comes is that the data types for the items involved in the chart are the same. Seriesttls is character and AnnualDifference (or A in the previous question) is numeric. Yet in the first I get no errors and in the second one, I do. Any thoughts on why this is?
In my experience, this error typically pops up if I got one of the parentheses wrong and am trying to add something within the call to ggplot. Your formatting makes this difficult to see, so let's look at this more nicely formatted:
jobgrowthbyindustry <-
ggplot(data=nvces2,
aes(y = AnnualDifference,
x = (reorder(Seriesttls,AnnualDifference))
)
+ geom_col(color="blue")
+ coord_flip()
+ labs(x=NULL)
+ ggtitle("Nevada Nonfarm Job Growth by Industry")
) + theme(plot.title.position = "plot",
plot.title = element_text(hjust =0.5)
)
One of the parentheses is misplaced.
It should be:
jobgrowthbyindustry <-
ggplot(data=nvces2,
aes(y = AnnualDifference,
x = (reorder(Seriesttls,AnnualDifference))
)
) +
geom_col(color="blue") +
coord_flip() +
labs(x=NULL) +
ggtitle("Nevada Nonfarm Job Growth by Industry") +
theme(plot.title.position = "plot",
plot.title = element_text(hjust =0.5)
)
And you can also remove the redundant () around the call to reorder.
If this is not resolving your issue, please do provide some data so that we can reproduce the error.
Related
I have a weird problem. I am using ggplot2 to generate 12 biplot figures for 12 RDA (Redundancy analysis). Please see the below code for one figure.
Here I attach the data and full code in the google drive https://drive.google.com/drive/folders/1ohO62-2jmMzVjJo2JLZOo8ffabzBx-8S?usp=sharing
plot1 <-ggplot() +
geom_point(data = sp,aes(RDA1,RDA2),shape=21,size=4,fill="tomato2")+
scale_fill_brewer(palette = 'Set2')+
geom_text_repel(data = sp,aes(RDA1,RDA2,label=sp2$Group),color="tomato1",size=4.5)+
geom_point(data = yz,aes(RDA1,RDA2),size=3,shape=24,fill="dodgerblue3")+
scale_shape_manual(values = c(22:23))+
geom_text_repel(data = yz,aes(RDA1,RDA2,label=yz2$label),color="dodgerblue2",size=4.5)+
labs(x=paste("RDA 1 (", format(100 *ii$cont[[1]][2,1], digits=3), "%)", sep=""),
y=paste("RDA 2 (", format(100 *ii$cont[[1]][2,2], digits=3), "%)", sep=""))+
geom_hline(yintercept=0,linetype=3,size=1,color="gray50") +
geom_vline(xintercept=0,linetype=3,size=1,color="gray50")+
guides(shape=guide_legend(title=NULL,color="black"),
fill=guide_legend(title=NULL))+
theme_bw()+theme(panel.grid=element_blank())+
theme(legend.position = "none") +
theme(legend.text = element_text(colour="black", size = 13)) +
theme(axis.title.x = element_text(colour="black",size=14),
axis.text.x = element_text(colour="black",size=14)) +
theme(axis.title.y = element_text(colour="black",size=14),
axis.text.y = element_text(colour="black",size=14))
plot1
The problem is that when I finished running the code for the first figure (plot1), I could see the first figure by running plot1. However, when after I finished running the code for the other figures (plot3 or 4 or 5..., not exact one, very randomly), I rerun plot1, it came out an error said Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (8): label
Run rlang::last_error() to see where the error occurred. Also, this kind of error comes out quite randomly. For plot1, sometimes there is no error. Sometimes this error shows.
When running the following with ggpubr, i receive the error
"Error in f(...) : Can only handle data with groups that are plotted on the x-axis"
my_comparisons <- list( c('A_1538_C', '1594delete12') )
ggplot(dt) +
geom_jitter(width = 0.1, height = 0, aes(
x=reorder(allele,mu_normal),
y=mu_normal)) +
theme_bw() +
theme(axis.text.x = element_text(angle =60, vjust = 1, hjust=1)) +
stat_compare_means(comparisons = my_comparisons, method = "t.test")
stat_compare_means is functioning in another plot of mine, and this is not the issue others have run into when x is an interaction of terms. (details on that issue here: https://github.com/const-ae/ggsignif/issues/16 )
the value of
unique(dt$allele)
is
[1] A_1714_C C_1721_A G_1546_T A_443_T C_1535_T A_1538_C
[7] 1594delete12 rpoB_1547A.G rpoB_1534T.C WT rpsL_128A.G rpsL_263A.G
[13] gyrA_248C_T
15 Levels: 1594delete12 A_1538_C A_1714_C A_443_T blank C_1535_T ... rpsL_263A.G
so it seems like my_comparisons is a set of existing terms? (I'd like to expand to more comparisons when it's working)
As pointed in the comments it seems that this messages come from not being able to find the right data for the comparison. A solution is to add the aesthetics in ggplot or duplicate it and add it to stat_compare_means besides the geom_* function used.
I would just like to simply add an annotation to my ggplot with the exponential function on it like this graph:
excel graph
Here is the data:Data
Here is the code I used thus far:
dfplot<-ggplot(data, aes(dilution.factor,Concentation)) +
geom_point(size=3)+ geom_smooth(method="auto",se=FALSE, colour="black")+
scale_y_continuous(breaks=seq(0,14,by=2))
dfplot2<-dfplot+labs(x=('Dilution Factor'), y=expression('Concentration' ~
(ng/mu*L)))+
theme_bw() + theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(colour="black"),
axis.line = element_line(colour = "black"))
dfplot3<- dfplot2+annotate("text", x=3, y=10, label = "R^2 == 1",parse=TRUE)
dfplot3
dfplot4<-dfplot3+annotate("text", x=3, y=11, label =
as.character(expression("y=13.048e^-{0.697x}" ,parse=TRUE)))
dfplot4
I can get all the way up to putting the r^2 value (dfplot3)dfplot3
For some reason I cannot get it to add the exponential equation in. I keep getting this error:
Error: Aesthetics must be either length 1 or the same as the data (1): label
What am i doing wrong?
Not quite sure about the as.character(expression()) syntax you are using, but when you are parsing annotation text, ggplot2 doesn't understand the 'human' style notation shortcut of placing a number next to a letter 13.084e, you need to tell it explicitly this is multiplication. You also need == instead of =.
annotate("text", x=3, y=11, label = "y == 13.048*e^-{0.697*x}", parse =TRUE)
Edit: I see that you have included parse = TRUE inside the expression call, I think this is a mistake. To do it with expression you would write the following, but this is not in fact necessary:
annotate("text", x=3, y=11, label = as.character(expression("y == 13.048*e^-{0.697*x}")), parse = T)
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 conduct a research about global education recently and the following graph is an important plot of my research.
ggplot(sam_data,aes(JOY,PV)) +
geom_line(aes(colour = Individualism))+
facet_grid(occupation~as.factor(Gender)) +
theme(legend.key.height = unit(2.0,"cm"),legend.text = element_text(size = 5,face = "plain")) +
scale_color_continuous("Individualism",labels=sam_data$country,breaks =sam_data$Individualism)+
geom_smooth()
And the problem is obvious :
1) The correlation line of different countries is all combined into one line, instead of different lines when segmented into gender and occupation.
2) The legend is a mess as I want to make it shown clear the countries corresponding to their individualism level. However, I tried to adjust many parameters of the legend and it did not work so much.
3) Also, I do not know how to delete the white gap produced by the breaks parameter. Any thoughts would be great!
I have solved the second problem by adjusting the aes parameter in ggplot function. The new code of mine is as follows
ggplot(sam_data,aes(JOYSCIE,PV1SCIE,group = CNTRYID)) +
geom_point(aes(color = Individualism.comp4))+
facet_grid(recode.OCOD3~as.factor(Gender0women1men)) +
theme(legend.key.height = unit(3.0,"cm"),legend.text = element_text(size = 5,face = "plain")) +
scale_color_gradientn("Individualism",labels=sam_data$CNTRYID,breaks =sam_data$Individualism.comp4,colors = rainbow(4))+
scale_x_continuous(limits = c(-2,2))