Tcl/Tk Referencing user-selected values in script - r

A quick question regarding Tcl/Tk and R. Below I have a slice of code that corresponds with a Tk button that executes the ToDo<-function(){...}.
My presumption is that both summary.myData() and ggplot() commands will execute. With this said, my question is whether or not I have correctly coded each command to reference the correct selected variable (the tk widget controlling this is working just fine, thus not depicted).
Namely if the arguments for each command are valid.
summary.myData<-summarySE(myData, measurevar=paste(tx.choice1), groupvars=paste(tx.choice2),conf.interval=0.95,na.rm=TRUE,.drop=FALSE)
would be read by R as
summary.myData<-summarySE(myData, measurevar=Measure, groupvars=Group,conf.interval=0.95,na.rm=TRUE,.drop=FALSE)
and
ggplot(data=summary.myData,aes(x=paste(tx.choice2),y=paste(tx.choice1)))+
geom_errorbar(aes(ymin=formula(paste(tx.choice1),"-ci"),ymax=formula(paste(tx.choice1),"+ci")), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title=paste("Interval Plot of",tx.choice1,"By",tx.choice2))
will be read as
ggplot(data=summary.mydata, aes(x=Group, y=Measure))+
geom_errorbar(aes(ymin=Measure-ci, ymax=Measure+ci), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title = "New plot title",x="X",y="y")
The slice for reference:
ToDo<-function(){
tx.choice1<<-vars.name.num[as.integer(tkcurselection(tl1))+1]
tx.choice2<<-vars.name.fac[as.integer(tkcurselection(tl2))+1]
numb.select.num<<-length(tx.choice1)
numb.select.fac<<-length(tx.choice2)
windows()
if (numb.select.num!=0){
if (numb.select.fac==0){
stop(tkmessageBox(message="Please select at least one categorical variable!", icon="error"))
}
else if (numb.select.fac==1) {
if(tclvalue(intervalplot_title)=="" & tclvalue(intervalplot_x)=="" & tclvalue(intervalplot_y)==""){
summary.myData<-summarySE(myData, measurevar=paste(tx.choice1), groupvars=paste(tx.choice2),conf.interval=0.95,na.rm=TRUE,.drop=FALSE)
ggplot(data=summary.myData,aes(x=paste(tx.choice2),y=paste(tx.choice1)))+
geom_errorbar(aes(ymin=formula(paste(tx.choice1),"-ci"),ymax=formula(paste(tx.choice1),"+ci")), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title=paste("Interval Plot of",tx.choice1,"By",tx.choice2))
} else {
summary.myData<-summarySE(myData, measurevar=paste(tx.choice1), groupvars=c(paste(tx.choice2)),conf.interval=0.95,na.rm=TRUE,.drop=FALSE)
ggplot(data=summary.myData,aes(x=paste(tx.choice2),y=paste(tx.choice1)))+
geom_errorbar(aes(ymin=paste(tx.choice1)-ci,ymax=paste(tx.choice1)+ci), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title=tclvalue(intervalplot_title),x=tclvalue(intervalplot_x),y=tclvalue(intervalplot_y))
}
tkinsert(txt,"end",paste("\nIntervalplot of", tx.choice1,"By",tx.choice2[1],"\n\n"))
}
}

Figured it out, the correct usage would just be paste(tx.choice1,"-ci"). There is no need to add the formula() condition.

Related

How I can correctly overlap bar and linechart together

I am using below codes
p <- ggplot() +
geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf),
stat="identity", position="dodge")+
geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean, colour=Leaf))+
geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean, group=Leaf))+
ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+
scale_y_continuous(sec.axis=sec_axis(~./20, name="2nd Y-axis"))+
theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")
graph produced from above code
I want graph like that
data
https://docs.google.com/spreadsheets/d/1Fjmg-l0WTL7jhEqwwtC4RXY_9VQV9GOBliFq_3G1f8I/edit#gid=0
From data, I want variable LA to left side and TT from right side
Above part is resolved,
Now, I am trying to put errorbars on the bar graph with below code, it caused an error, can someone have a look for solution?
p + geom_errorbar(aes(ymin=Mean-se, ymax=Mean+se), width=0.5,
position=position_dodge(0.9), colour="black", size=.7)
For this you need to understand that even you have the second Y-Axis, it is just a markup and everything draw on the graph is still base on the main Y-Axis(left one).
So you need to do two things:
Convert anything that should reference to the second Y-Axis to same scale of the one on the left, in this case is the bar scale (LA variables) whose maximum is 15. So you need to divide the value of TT by 20.
Second Axis needs to label correctly so it will be the main Y-Axis multiply by 20.
p <- ggplot() +
geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf),
stat="identity", position="dodge") +
# values are divided by 20 to be in the same value range of bar graph
geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean/20, colour=Leaf))+
geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean/20, group=Leaf))+
ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+
# second axis is multiply by 20 to reflect the actual value of lines & points
scale_y_continuous(
sec.axis=sec_axis(trans = ~ . * 20, name="2nd Y-axis",
breaks = c(0, 100, 200, 300))) +
theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")
For the error par which is very basic here. You will need to adjust the theme and the graph to have a good looking one.
p + geom_errorbar(data = filter(df, variable=="TT"),
aes(x = Gen, y=Mean/20, ymin=(Mean-se)/20,
ymax=(Mean+se)/20), width=0.5,
position=position_dodge(0.9), colour="black", size=.7)
One final note: Please consider reading the error message, understand what it say, reference to the help document of packages, functions in R so you can learn how to do all the code yourself.

R studio graph not plotted (neither with x11())

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.

ggplot2 facet_wrap doesn't find a variable but shape does

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) +
...

ggplot2: multiple colours in stat_summary

I have a plot in which I am displaying individual values from multiple subjects, coloured by group. Added to that are means per group, calculated using stat_summary.
I would like the two means to be coloured by group, but in colours other than the individual data. This turns out to be difficult, at least when using stat_summary. I have the following code:
ggplot(data=dat,
aes(x=Round, y=DV, group=Subject, colour=T1)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", color="black",
aes(group=T1))
Which produces this example graph.
The colour for the means created by stat_summary is set to black; otherwise it would be red and blue like the individual data lines. However, it is not possible to set more than one colour - so color=c("black", "blue") does not work.
I've already tried scale_colour_manual as explained here, but this will change the colours of the individual data lines, leaving the mean lines unaffected.
Any suggestion how to solve this? Code and data here.
You need to create different values for the mapping to color:
ggplot(data=iris,
aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", aes(color=paste("mean", Species)))
You can then use scale_color_manual to get specific colors.

Apply coord_flip() to single layer

I would like to have a boxplot showing the same distribution underneath my histogram.
The code below almost works, but coord_flip() is being applied to all layers, instead of just the geom_boxplot layer.
plot1<-ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation])) +
xlab(GGVar) + ylab("Proportion of Instances") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white",origin=-0.5) +
scale_x_continuous(limits=c(-3,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
geom_boxplot(aes_string(x=-1, y=newdatahistogram[RawLocation])) + coord_flip()
How can I apply coord_flip() to a single layer?
Thank you!
I got it to work with a bit of a hack;
plot1 <- ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation], fill=(newdatahistogram[,"PQ"]))) +
xlab(GGVar) + ylab("Proportion of Observation") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", origin=-0.5) +
scale_x_continuous(limits=c(-1,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
scale_y_continuous(limits=c(-.2,1), breaks=seq(0,1,by=.2))
theme(plot.margin = unit(c(0,0,0,0), "cm"))
plot_box <- ggplot(newdatahistogram) +
geom_boxplot(aes_string(x=1, y=newdatahistogram[RawLocation])) +
scale_y_continuous(breaks=(0:5), labels=NULL, limits=c(-1,6), expand=c(.0,-.03)) +
scale_x_continuous(breaks=NULL) + xlab(NULL) + ylab(NULL) +
coord_flip() + theme_bw() +
theme(plot.margin = unit(c(0,0,.0,0), "cm"),
line=element_blank(),text=element_blank(),
axis.line = element_blank(),title=element_blank(), panel.border=theme_blank())
PB = ggplotGrob(plot_box)
plot1 <- plot1 + annotation_custom(grob=PB, xmin=-1.01, xmax=5.95, ymin=-.3,ymax=0)
This saves the rotated boxplot as a grob object and inserts it into the plot under the histogram.
I needed to play with the expansion element a bit to get the scales to line up,
but it works!
Seriously though, I think ggplot should have a horizontal boxplot available without cord_flip()... I tried to edit the boxplot code, but it was way too difficult for me!
Tried to post image, but not enough reputation
You can't: coord_flip always acts on all layers. However, you do have two alternatives:
The solution here shows how to use grid.arrange() to add a marginal histogram. (The comments in the question also link to a nice base-R way to do the same thing)
You could indicate density using a rug plot on of the four sides of the plot with plot1 + geom_rug(sides='r')
ggplot(mpg, aes(x=class, y=cty)) +
geom_boxplot() + geom_rug(sides="r")

Resources