why the 'fill=' function doesnt work in boxplot in ggplots? - r

I am making boxplot by ggplot2, but I want to divide into two groups, treated' and 'control', so I use 'fill=treatment', but still one box in each time point,
however, when I use 'fill=treatment' in barplot, it works,
so can you help me to fix it, really thanks!
newcrk10m <- melt(newcrk10,id.vars="time point",variable.name="treatment",
value.name="value")
ggplot(newcrk10m,aes(`time point`,value,fill=treatment))+
geom_bar(stat="identity",position="dodge")+
scale_x_continuous(breaks = seq(0,72,24))
ggplot(newcrk10m,aes(x=`time point`,y=value,
group=`time point`,fill=treatment))+
geom_boxplot(size=0.5)+scale_x_continuous(breaks = seq(0,72,24))

i fix it, i paste 'time point' and 'treatment' then make a new df, it works, thanks!

Related

Changing the Order of Levels through ggplot

I am trying to learn the package cregg through the tutorial here. The tutorial works fine. However, I have an issue when I try to change the default setting of the functions. It looks like when it plots, the order of the levels and coef dots of the legend is ordered alphabetically or by numbers. My question is that when I have tried two ways: one if through the ggplot function and the second one is to change the order of levels in advance to change the order to, say 31524, both methods do not work. The original codes are as follow:
data("immigration")
stacked <- cj(immigration, ChosenImmigrant ~ Gender +
Education + LanguageSkills + CountryOfOrigin + Job + JobExperience +
JobPlans + ReasonForApplication + PriorEntry, id = ~ CaseID,
estimate = "mm", by = ~ contest_no)
plot(stacked, group = "contest_no", feature_headers = FALSE)
My question is how I can the order of levels of contest_no both on the plot and in the legend. One thing I have found is that it seems like the order of levels of contest_no is determined by the function cj first (you can check it by stacked[["contest_no"]]). Thank you!
Thanks to #Tung!(I know I left a similar comment but I still want to answer this one and close it) The answer is simple and straightforward but I didn't think it completely. In my question I kind of having the answer but I didn't know why I didn't see it. Since stacked[["contest_no"]] can show the order of levels of stacked[["contest_no"]], I just change the order by stacked[["contest_no"]] <- factor(stacked[["contest_no"]], levels=c(3, 1, 5, 2, 4)) and then plot the whole object of stacked. It works fine.

Mosaic plot and text values

I created structable from Titanic dataset and used mosaic function for it. Everything worked great, hovewer I also wanted to label each box from mosaic plot with quantity of titanic passangers given their Class, Survival and Sex. As it turns out, I am not able to do that. I know I need to use labeling_cells to achive that, hovewer i am not able to use it (and i wan't able to find any example) in combination with stuctable and below code.
library("vcd")
struct <- structable(~ Class + Survived + Sex, data = Titanic)
mosaic(struct, data = Titanic, shade = TRUE, direction = "v")
If I understand your question correctly, then the last example in ?labeling_cells is pretty close to what you want to do. Using your example, the labeling_cells() can be added afterwards provided that the viewport tree is not popped. The only aspect that is somewhat awkward is that the struct object has to be a regular table again for the labeling. I have to ask David, the main author, whether this could be handled automatically.
mosaic(struct, shade = TRUE, direction = "v", pop = FALSE)
labeling_cells(text = as.table(struct), margin = 0)(as.table(struct))
Fixed in upstream in vcd 1.4-4, but note that you can simply use
mosaic(struct, labeling = labeling_values)

Geom_points not dodging when geom_errorbars are

I can't figure out how to get these geom_points to properly dodge! I've searched many, MANY how-to's and questions on different stackexchange pages, but none of them fix the problem.
analyze_weighted <- data.frame(
mus = c(clean_mu,b_mu,d_mu,g_mu,bd_mu,bg_mu,dg_mu,bdg_mu,m_mu),
sds = c(clean_sigma,b_sigma,d_sigma,g_sigma,bd_sigma,bg_sigma,dg_sigma,bdg_sigma,m_sigma),
SNR =c("No shifts","1 shift","1 shift","1 shift","2 shifts","2 shifts","2 shifts","3 shifts","4 shifts"),
)
And then I try to plot it:
ggplot(analyze_weighted, aes(x=SNR,y=mus,color=SNR,group=mus)) +
geom_point(position="dodge",na.rm=TRUE) +
geom_errorbar(position="dodge",aes(ymax=mus+sds/2,ymin=mus-sds/2,), width=0.25)
And it manages to dodge the error bars but not the points! I'm going crazy here, what do I do?
Here's what it looks like now--I want the points to be slightly dodged!
geom_point requires that you explicitly provide the width you desire the points to dodge.
This should work:
ggplot(analyze_weighted, aes(x=SNR,y=mus,color=SNR,group=mus)) +
geom_point(position=position_dodge(width=0.2),na.rm=TRUE) +
geom_errorbar(position=position_dodge(width=0.2),aes(ymax=mus+sds/2,ymin=mus-sds/2),width=0.25)
Please notice that your example wasn't a fully reproducible one, as no values of the variables used to construct mus and sds are available.

combine two plots in R . second plot is repalcing the first one

#qcc package(spc charts)
library(qcc)
A <- c(10,20,30)
B <- c(25,35,44)
par(mfrow = c(2, 1))
qcc(A,type="xbar.one")
qcc(B,type="xbar.one")
problem is : chart(B) is replacing chart(A) instead of positiong in the second row.
someone please let me know how to overcome these problem
There probably is a kind of bug and also, you need to use plot.
Here is a solution to make it work :
qA<-qcc(A,type="xbar.one")
qB<-qcc(B,type="xbar.one")
par(mfrow=c(2,1))
plot(qA, restore.par=FALSE)
plot(qB)
The strange (probably bug?) part is that doing the below thing doesn't work... :
par(mfrow=c(2,1))
plot(qcc(A,type="xbar.one"),restore.par=FALSE)
plot(qcc(B,type="xbar.one"))

ggplot2 & stat_ellipse: Draw ellipses around multiple groups of points

This might be a simple one, but I'm trying to draw ellipses around my treatments on my PCoA plot.
My data frame (sc) is:
MDS1 MDS2 Treatment
X1xF1 -0.19736183 -0.24299825 1xFlood
X1xF2 -0.17409568 -0.29727596 1xFlood
X1xF3 -0.15272444 -0.28553837 1xFlood
S1 -0.06643271 0.47049959 Start
S2 -0.15143350 0.31152966 Start
S3 -0.26156297 0.12296849 Start
X3xF1 0.29840827 0.04581617 3xFloods
X3xF2 0.50503749 -0.07011503 3xFloods
X3xF3 0.20016537 -0.05488630 3xFloods
and my code is:
ggplot(data=sc,(aes(x=MDS1,y=MDS2,colour = Treatment)))+geom_point(size=3)+
ggtitle("PCoA of samples at 'class' level(method='Bray')\n",sep=''))+
theme_bw()+guides(colour = guide_legend(override.aes = list(size=3)))+
stat_ellipse()
It plots the PCoA okay up until stat_ellipse(). I've tried it with various parameters and at best I can get one ellipse for the whole plot (although I can't seem to reproduce that now).
What I'm after is three CI ellipses for the three treatments, coloured the same as the treatments. Any help would be very appreciated!
Thanks.
There is no stat_ellipse(...) in the ggplot package, so you must have retreived it from somewhere else. Care to share?? There are at least two versions that I am aware of, here, and here. Neither of these seem to work with your dataset, which is odd because both have worked with other datasets.
I finally fell back on the option of generating the ellipses externally to ggplot, which is not that difficult really.
library(ggplot2)
library(ellipse)
centroids <- aggregate(cbind(MDS1,MDS2)~Treatment,sc,mean)
conf.rgn <- do.call(rbind,lapply(unique(sc$Treatment),function(t)
data.frame(Treatment=as.character(t),
ellipse(cov(sc[sc$Treatment==t,1:2]),
centre=as.matrix(centroids[t,2:3]),
level=0.95),
stringsAsFactors=FALSE)))
ggplot(data=sc,(aes(x=MDS1,y=MDS2,colour = Treatment)))+
geom_point(size=3)+
geom_path(data=conf.rgn)+
ggtitle(paste("PCoA of samples at 'class' level(method='Bray')\n",sep=''))+
theme_bw()+
guides(colour = guide_legend(override.aes = list(size=3)))

Resources