Changing the Order of Levels through ggplot - r

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.

Related

Customize Barplot in base function barplot()

I have a date frame (df), with 2 columns: One numerical and one as.factor() with three levels:
Pre
Post
Blank
I want to make a barplot() with each factor colored to it's respective group (easy), and change the order of the plot so each factor appears next to each other (this is where I'm stuck).
I followed the same logic as I would with a boxplot(), but it does not appear to work the same. I also tried following examples from several stackoverflow threads, including (but not limited to) this one:
Re-ordering bars in R's barplot()
But still can't get it to work.
Here is what I've tried, and it works with the boxplot function quite well:
df <- read.table("https://pastebin.com/raw/zaETq28M", header = T)
df$Treatment <- as.factor(df$Treatment)
levels(df$Treatment) # note: I would like to display order to be: Pre, Post, then Blank.
df$Treatment <- ordered(df$Treatment, levels = c("Pre","Post","Blank")) # set to the right order
barplot(df$Cq,names.arg = df$Treatment ,col = df$Treatment, ylim=c(0,30), main = "Not the right order bar plot", cex.main=2)
In total, I should have 66 individual bars (which I do), but somehow, the order of the graph is not what I set, and the groups are still separated. How can I simply get 3 distinct groups? Meaning, first show all "Pre", then all "post", followed by "blank"
General questions for future posts:
How to get a get my graphs to be displayed on Stackoverflow when I post a question? For some reason, my posts never include my graphs.
Also, any kind suggestion on using color blind pallet would be great, but I can just do this manually if needed. Just curious if there is an automatic way of doing it, so I do not need to set it manually in all my graphs
Thank you for your help
Do you mean this?
First the Pre, then Post then blank. Within each group order is preserved. Legend added with blank == No Treatment.
df <- read.table("https://pastebin.com/raw/zaETq28M", header = T)
df_Pre <- df[which(df$Treatment == 'Pre'),]
df_Post <- df[which(df$Treatment == 'Post'),]
df_Blank <- df[which(df$Treatment == 'Blank'),]
ddf <- rbind(df_Pre, df_Post, df_Blank)
ddf$color <- c(rep('blue', nrow(df_Pre)), rep('red', nrow(df_Post)), rep('magenta', nrow(df_Blank)))
barplot(ddf$Cq, col = ddf$color, names = rownames(ddf))
legend("bottomleft",
legend = c("Pre-Treatmen", "Post-Treatment", 'No Treatment'),
fill = c("darkblue", "red","magenta"))

How do I use the group argument for the plot_summs() function from the jtools package?

I am plotting my coefficient estimates using the function plot_summs() and would like to divide my coefficients into two separate groups.
The function plot_summs() has an argument groups, however, when I try to use it as explained in the documentation, I do not get any results nor error. Can someone give me an example of how I can use this argument please?
This is the code I currently have:
plot_summs(model.c, scale = TRUE, groups = list(pane_1 = c("AQI_average", "temp_yearly"), pane_2 = c("rain_1h_yearly", "snow_1h_yearly")), coefs = c("AQI Average"= "AQI_average", "Temperature (in Farenheit)" = "temp_yearly","Rain volume in mm" = "rain_1h_yearly", "Snow volume in mm" = "snow_1h_yearly"))
And the image below is what I get as a result. What I would like to get is to have two panes separate panes. One which would include "AQI_average" and "temp_yearly" and the other one that would have "rain_1h_yearly" and "snow_1h_yearly". Event though I use the groups argument, I do not get this.
Output of my code
By minimal reproducible example, markus is refering to a piece of code that enables others to exactly reproduce the issue you are refering to on our respective computers, as described in the link that they provided.
To me, it seems the problem is that the groups function does not seem to work in plot_summs - it seems someone here also pointed it out.
If plot_summs is replaced by plot_coef, the groups function work for me. However, the scale function does not seem to be available. A workaround might be:
r <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris)
y <- plot_summs(r, scale = TRUE) #Plot for scaled version
t <- plot_coefs(r, #Plot for unscaled versions but with facetting
groups =
list(
pane_1 = c("Sepal.Width", "Petal.Length"),
pane_2 = c("Petal.Width"))) + theme_linedraw()
y$data$group <- t$data$group #Add faceting column to data for the plot
t$data <- y$data #Replace the data with the scaled version
t
I hope this is what you meant!

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

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!

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.

ggplot2 equivalent of 'factorization or categorization' in googleVis in R

Due to static graph prepared by ggplot, we are shifting our graphs to googleVis with interactive charts. But when it comes to categorization we are facing many problems. Let me give example which will help you understand:
#dataframe
df = data.frame( x = sample(1:100), y = sample(1:100), cat = sample(c('a','b','c'), 100, replace=TRUE) )
ggplot2 provides parameter like alpha, colour, linetype, size which we can use with categories like shown below:
ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))
Not just line chart, but majority of ggplot2 graphs provide categorization based on column values. Now I would like to do the same in googleVis, based on value df$cat I would like parameters to get changed or grouping of line or charts.
Note:
I have already tried dcast to make multiple columns based on category column and use those multiple columns as Y input, but that it not what I would like to do.
Can anyone help me regarding this?
Let me know if you need more information.
vrajs5 you are not alone! We struggled with this issue. In our case we wanted to fill bar charts like in ggplot. This is the solution. You need to add specifically named columns, linked to your variables, to your data table for googleVis to pick up.
In my fill example, these are called roles, but once you see my syntax you can abstract it to annotations and other cool features. Google has them all documented here (check out superheroes example!) but it was not obvious how it applied to r.
#mages has this documented on this webpage, which shows features not in demo(googleVis):
http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html
EXAMPLE ADDING NEW DIMENSIONS TO GOOGLEVIS CHARTS
# in this case
# How do we fill a bar chart showing bars depend on another variable?
# We wanted to show C in a different fill to other assets
suppressPackageStartupMessages(library(googleVis))
library(data.table) # You can use data frames if you don't like DT
test.dt = data.table(px = c("A","B","C"), py = c(1,4,9),
"py.style" = c('silver', 'silver', 'gold'))
# Add your modifier to your chart as a new variable e.g. py1.style
test <-gvisBarChart(test.dt,
xvar = "px",
yvar = c("py", "py.style"),
options = list(legend = 'none'))
plot(test)
We have shown py.style deterministically here, but you could code it to be dependent on your categories.
The secret is myvar.googleVis_thing_youneed linking the variable myvar to the googleVis feature.
RESULT BEFORE FILL (yvar = "py")
RESULT AFTER FILL (yvar = c("py", "py.style"))
Take a look at mages examples (code also on Github) and you will have cracked the "categorization based on column values" issue.

Resources