Facet for continuous variables in ggplot2 [duplicate] - r

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
ggplot - facet by function output
ggplot2's facets option is great for showing multiple plots by factors, but I've had trouble learning to efficiently convert continuous variables to factors within it. With data like:
DF <- data.frame(WindDir=sample(0:180, 20, replace=T),
WindSpeed=sample(1:40, 20, replace=T),
Force=sample(1:40, 20, replace=T))
qplot(WindSpeed, Force, data=DF, facets=~cut(WindDir, seq(0,180,30)))
I get the error : At least one layer must contain all variables used for facetting
I would like to examine the relationship Force~WindSpeed by discrete 30 degree intervals, but it seems facet requires factors to be attached to the data frame being used (obviously I could do DF$DiscreteWindDir <- cut(...), but that seems unecessary). Is there a way to use facets while converting continuous variables to factors?

Making an example of how you can use transform to make an inline transformation:
qplot(WindSpeed, Force,
data = transform(DF,
fct = cut(WindDir, seq(0,180,3))),
facets=~fct)
You don't "pollute" data with the faceting variable, but it is in the data frame for ggplot to facet on (rather than being a function of columns in the facet specification).
This works just as well in the expanded syntax:
ggplot(transform(DF,
fct = cut(WindDir, seq(0,180,3))),
aes(WindSpeed, Force)) +
geom_point() +
facet_wrap(~fct)

Related

How can I organise my x-axis variables by another factor? [duplicate]

This question already has an answer here:
ggplot facet by column
(1 answer)
Closed 11 months ago.
I'm very new to R/ggplot so thanks in advance for your patience. Here's what I have at the moment:
ggplot(data = figure_data_3A,
mapping = aes(x = `Gene`,
y = `Percent growth`)
)+
geom_col()+
theme_classic()+
ylab("% Growth of double relative to single mutants")+
xlab(NULL)+
theme(axis.text.x = element_text(angle = 90))
Here's my code, and I want to organise the elements on the x-axis by another qualitative factor in my dataframe called Function/Process, so that I can label them together in groups, ultimately to look like .
Without a reproducible example (we cannot see your data), we have to guess.
But generally, when plotting a character (text) variable in ggplot2, it gets converted to a factor, and the order of that factor is simply the sorted elements.
Instead, you can preprocess your data, convert Gene to a factor and specify the order.
I think something akin to this would do:
# order your data jf. Function/Process
figure_data3A <- figure_data3A[order(figure_data3A$`Function/Process`),]
# make a factor,
figure_data_3A$Gene <- factor(figure_data_3A$Gene, levels=figure_data_3A$Gene)
In the last line, I am assuming that each gene only appears once in your data frame.

Vizualize two step cluster using ggplot2 [duplicate]

This question already has an answer here:
Reversed order of bars in grouped barplot with coord_flip
(1 answer)
Closed 2 years ago.
From spss there is a kind of clustering which is called two step cluster.
The vizual option is provided by spss is something like this left side plot.
Having the results of clusters, label/names of the variables used and their score into a dataframe like this
data.frame(cluster = c(1,1,1,2,2,2,3,3,3), value = c("Google","Amazon","Yahoo","Google","Amazon","Yahoo","Google","Amazon","Yahoo"), score = c(2194.2,43.2,4331.3,31.3,133.1,432.1,3234.1,44.3,21.4))
These are the inputs as refered in the spss plot.
is there any efficient way to vizualize them using ggplot2?
Maybe something like this:
library(ggplot2)
#Plot
ggplot(df,aes(x=cluster,y=score,fill=value))+geom_bar(stat='identity',position = 'stack')+
coord_flip()

Avoid overlapping x-axis labels with ggplot? [duplicate]

I'm having some trouble with qplot in R. I am trying to plot data from a data frame. When I execute the command below the plot gets bunched up on the left side (see the image below). The data frame only has 963 rows so I don't think size is the issue, but I can use the same command on a smaller data frame and it looks fine. Any ideas?
library(ggplot2)
qplot(x=variable,
y=value,
data=data,
color=Classification,
main="Average MapQ Scores")
Or similarly:
ggplot(data = data, aes(x = variable, y = value, color = Classification) +
geom_point()
Your column value is likely a factor, when it should be a numeric. This causes each categorical value of value to be given its own entry on the y-axis, thus producing the effect you've noticed.
You should coerce it to be a numeric
data$value <- as.numeric(as.character(data$value))
Note that there is probably a good reason it has been interpreted as a factor and not a numeric, possibly because it has some entries that are not pure numeric values (maybe 1,000 or 1000 m or some other character entry among the numbers). The consequence of the coercion may be a loss of information, so be warned or cleanse the data thoroughly.
Also, you appear to have the same problem on the x-axis.

simple boxplot using qplot/ggplot2

This is my first post, so go easy. Up until now (the past ~5 years?) I've been able to either tweak my R code the right way or find an answer on this or various other sites. Trust me when I say that I've looked for an answer!
I have a working script to create the attached boxplot in basic R.
http://i.stack.imgur.com/NaATo.jpg
This is fine, but I really just want to "jazz" it up in ggplot, for vain reasons.
I've looked at the following questions and they are close, but not complete:
Why does a boxplot in ggplot requires axis x and y?
How do you draw a boxplot without specifying x axis?
My data is basically like "mtcars" if all the numerical variables were on the same scale.
All I want to do is plot each variable on the same boxplot, like the basic R boxplot I made above. My y axis is the same continuous scale (0 to 1) for each box and the x axis simply labels each month plus a yearly average (think all the mtcars values the same on the y axis and the x axis is each vehicle model). Each box of my data represents 75 observations (kind of like if mtcars had 75 different vehicle models), again all the boxes are on the same scale.
What am I missing?
Though I don't think mtcars makes a great example for this, here it is:
First, we make the data (hopefully) more similar to yours by using a column instead of rownames.
mt = mtcars
mt$car = row.names(mtcars)
Then we reshape to long format:
mt_long = reshape2::melt(mt, id.vars = "car")
Then the plot is easy:
library(ggplot2)
ggplot(mt_long, aes(x = variable, y = value)) +
geom_boxplot()
Using ggplot all but requires data in "long" format rather than "wide" format. If you want something to be mapped to a graphical dimension (x-axis, y-axis, color, shape, etc.), then it should be a column in your data. Luckily, it's usually quite easy to get data in the right format with reshape2::melt or tidyr::gather. I'd recommend reading the Tidy Data paper for more on this topic.

Pairs scatter plot; one vs many [duplicate]

This question already has answers here:
Plot one numeric variable against n numeric variables in n plots
(4 answers)
Closed 5 years ago.
Is there a parsimonious way to create a pairs plot that only compares one variable to the many others? In other words, can I plot just one row or column of the standard pairs scatter plot matrix without using a loop?
Melt your data then use ggplot with facet.
library("ggplot2")
library("reshape2")
#dummy data
df <- data.frame(x=1:10,
a=runif(10),
b=runif(10),
c=runif(10))
#melt your data
df_melt <- melt(df,"x")
#scatterplot per group
ggplot(df_melt,aes(x,value)) +
geom_point() +
facet_grid(.~variable)
I'll round it out with a base plotting option (using df from #zx8754):
layout(matrix(seq(ncol(df)-1),nrow=1))
Map(function(x,y) plot(df[c(x,y)]), names(df[1]), names(df[-1]))
Although arguably this is still a loop using Map.
For the fun, with lattice (with #zx8754 "df_melt"):
library(lattice)
xyplot(value ~ x | variable, data = df_melt, layout = c(3,1),
between = list(x=1))

Resources