How do I Order Boxes on One boxplot graph? (r) - r

I've read the answers to several questions of this type, so sorry for the repeat, but I had trouble understanding how they applied to how my data is laid out.
What I would like is to order the boxes on my boxplot (which has, say, four boxes) in the order I wish as opposed to alphabetically.
This is a simplified version of my current code:
TotalPer = c(1, 4, 6, 17, 4, 12)
IntPer = c(3, 8, 10, 1, 4, 8)
DomPer = c(4, 5, 10, 20, 13, 12)
IntDomBox <- data.frame(y=c(TotalPer,IntPer,DomPer),
x=c(rep("Total",length(TotalPer)),rep("International",length(IntPer)),
rep("Domestic",length(DomPer))))
with(IntDomBox, boxplot(y~x, main = "Prediction Residuals", ylab="%",
par(cex.axis=0.7)))
This produces a boxplot in alphabetical order. If I prefer to have the boxplot in the order listed in the dataframe (Total, Int, Dom) how would I do this? I know about making it reverse alphabetical instead, but in some cases that is not what I want, so I'd prefer to be able to manually assign the order.
Thanks!

One possibility out of many: Reorder the factor levels before plotting by executing IntDomBox$x <- factor(IntDomBox$x, levels=unique(IntDomBox$x))

Related

ggplot2: Centering stacked barplot on central category

I'm creating a stacked barplot where I have 5 ordered categories for each bar - let's just call them 1, 2, 3, 4, 5. I want to center each bar around the middle value (3), in a similar format to how O'Connor et. al. did here.
I have the rest of the figure completed.
Here's an example of my data and what I have so far:
sampledata=data.frame(c(rep("category1",30),rep("category2",30),rep("category3",30)),round(runif(90,min=1,max=5)))
colnames(sampledata)=c("categories","values")
ggplot(data=sampledata,aes(x=categories,fill=factor(values)))+
geom_bar(position="stack")+
scale_fill_brewer(palette="RdBu",direction=-1)+
coord_flip()
I'm not sure how exactly to word this problem and searches so far have not been conclusive. Thanks for your help!
Let's set up your sample data differently:
set.seed(1001)
sampledata <- data.frame(category1 = sample(factor(1:5), 30, replace = TRUE),
category2 = sample(factor(1:5), 30, replace = TRUE),
category3 = sample(factor(1:5), 30, replace = TRUE))
Now it is in suitable form for use with the likert package.
library(likert)
plot(likert(sampledata))

What am I doing wrong with dygraph's showZeroValues option?

I'm trying to plot several data series onto the same plot in R, but even with the showZeroValues=TRUE argument in dyLegend(), the legend stops showing values on mouseover when at least one of the series has a y=0 at the current x. I am not sure what I am doing wrong.
Below is a simplified example:
library(dygraphs)
library(xts)
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x, order.by=x$Date)[,1:2]
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)
On my computer the dynamic legend skips all x values at which one of the two series has y=0, as can be seen with the cursor being close to zeros but the legend still stuck on the right end of the graph: example.
I had the same issue and found out that it was caused by the xts object containing character strings. The original data frame had a Date column, which I used to create the xts object, but I did not subset the numerical data. This resulted in the xts object being created but with character values (see issue here). Surprisingly enough, the resulting plots were not much impacted, and the output was correct, which made troubleshooting less straightforward.
In your example, the following should solve the issue:
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x[, 1:2], order.by=x$Date) # This is the only change in your code
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)

R - Creating a fuzzy correspondance analysis

I have ecological data of benthic invertebrate species traits. I would like to do a fuzzy correspondance analysis with ade4-package. I have trait sub-categories as columns and benthic sampling places as rows.
I have prepared and plotted a matrix of fuzzy variables with my "painotettu" dataframe using prep.fuzzy.var from ade4-package.
library("ade4", lib.loc="~/Library/R/3.0/library")
w <- prep.fuzzy.var(
painotettu,
c(5, 5, 6, 4, 3, 4, 2, 3, 3,
5, 5, 5, 5, 3, 6, 3, 5, 3)
)
scatter(dudi.fpca(w, scann = FALSE, nf = 3), csub = 2, type = 0, clab.moda = 2)
The result:
The result is very unclear and to sort it out, I have been trying to:
Separate the places into one picture and traits into another
Delete the text boxes so that the names could be seen more easily.
I'm also searching for some function that would compute a matrix that would give numerical values of what the arrows represent.
I'm trying to sort the data to look more like this:
Please let me know if any further clarification is needed.

make a figure in R with two panels in the top row and three in the bottom row [duplicate]

This question already has answers here:
Arrange plots in a layout which cannot be achieved by 'par(mfrow ='
(2 answers)
Closed 7 years ago.
I would really like to make a figure in R that has two panels in the top row and three in the bottom row. I would like to pull this off using the base graphics package. I do not want to use ggplot. Thoughts?
plot.mat = matrix(c(1, 1, 1, 2, 2, 2,
3, 3, 4, 4, 5, 5),
nrow = 2, byrow = T)
layout(plot.mat)
layout.show(n = 5)
# looks good
for (i in 1:5) plot(rnorm(10), rnorm(10))
The ?layout help is well-written and has plenty of examples.

R- pareto chart grouping like histogram

I need to create a Pareto chart in R. From the example of "qcc" library I need to do grouping before:
let's suppose my table is:
defect <- data.frame(a=c(8, 7, 6, 4, 3, 3, 3, 0, 0, 1))
If a do a histogram I get the grouping automatically
hist(defect$a, breaks=c(-1:8))
But with a pareto graph I don't:
pareto.chart(defect$a, ylab = "Error frequency")
Is there a way to get the grouping and the chart without having to group it with ddply?
I need to get the same result of the following, but without having to group it manually.
bb<-ddply(defect$a, .(a), count)
pareto.chart(bb$a, ylab = "Error frequency")
From the documentation it seems pretty clear that the pareto.chart function expects summarized data. If you don't want to use ddply() you could use the base table() function
pareto.chart(table(defect$a), ylab = "Error frequency")

Resources