I want to make a strip chart for each of the following two vectors with same x-axis using R.
Two vectors are:
x <- c(40,35,30,45,35,45,65,65,70,70)
y <- c(45,45,45,45,45,45,45,45,45,95)
When I made strip charts for the two, it came out like this:
How do I make it so that two x-axis will be the same?
Thank you
How do I make it so that two x-axis will be the same?
The stripchart() function takes an optional xlim parameter that lets you define the horizontal plot limits. For example:
x <- c(40,35,30,45,35,45,65,65,70,70)
y <- c(45,45,45,45,45,45,45,45,45,95)
par(bty="n", mfrow=c(2,1), mar=c(2,1,3,1)+0.1)
stripchart(x, xlim=c(20,100), method="stack", pch=16)
stripchart(y, xlim=c(20,100), method="stack", pch=21)
Related
I am working in R and I have to make many boxplots. This is a visualization of group differences. I want to relabel the x-axis to only have one title instead of five (one for each subplot). My biggest problem is that I also want the y-axis of all the subplots to have different labels.
This is what I tried so far:
par(mfrow=c(1,5))
lapply(NEW8[,c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")],
function(x) boxplot(x ~ NEW8$PAPA_p4_adhd,col=rainbow(2),
names=c("CN","ADHD"),
ylab=c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")))
All the y-labels are added to each subplots so each subplots has 5 lines of y-axis labels (gawayf, humf, sgamef, swtoyf, kissf), and each plot says what data was used to create the boxplot (PAPA_P4_ADHD).
I want each plots to only have the corresponding y-axis label and the x-axis to have 1 label for all five plots.
This is my current output:
Thank you very much
Instead of lapply try mapply - that will allow to pass different argument to each function call:
par(mfrow=c(1,5))
myBox <- function(x, y, ...) boxplot(x ~ y, col=rainbow(2), names=c("CN", "ADHA"), ...)
mapply(myBox,
x = NEW8[,c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")],
y = list(NEW8$PAPA_p4_adhd), # we make this a list so it has length(1)
ylab = c("gawayf", "humf", "sgamesf", "swtoyf", "kissf"),
xlab = "" # empty x-lab
)
For x-lab you will have to do a trick - start a new empty plot that overlays all of the plots, and only add x-axis:
par(fig=c(0,1,0,1), oma=c(0,0,0,0), mar=par("mar"), new=TRUE)
plot.new()
title(xlab="my x-axis")
NOTE: I didn't try to run this code myself, if anything here doesn't work - please leave a comment and will try to address it.
I'm plotting a graph using this
plot(dates,returns)
I would like to have the returns expressed as percentages instead of numbers. 0.1 would become 10%. Also, the numbers on the y-axis appear tilted 90 degrees on the left. Is it possible to make them appear horizontally?
Here is one way using las=TRUE to turn the labels on the y-axis and axis() for the new y-axis with adjusted labels.
dates <- 1:10
returns <- runif(10)
plot(dates, returns, yaxt="n")
axis(2, at=pretty(returns), lab=pretty(returns) * 100, las=TRUE)
If you use ggplot you can use the scales package.
library(scales)
plot + scale_y_continuous(labels = percent)
library(scales)
dates <- 1:100
returns <- runif(100)
yticks_val <- pretty_breaks(n=5)(returns)
plot(dates, returns, yaxt="n")
axis(2, at=yticks_val, lab=percent(yticks_val))
Highlights:
No need to explicitly add "%"
Manually fix the number of y-ticks to be consistent with further plots. Here I chose 5.
Combining two answers together #rengis #vladiim
i have this data and plot
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),e=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T,main='plot',pch=19)
I would like to subset the x axis into two labels named 'a+b' and 'c+d' labels
Thanks in advance
In your case you can simply use mtext on side 1:
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),d=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T,main='plot',pch=19)
mtext(c('a+b','c+d'),side=1,line=3,at=c(1.5,3.5))
Argument line is to set up the vertical position and at the position on the x-axis.
Edit: To add a distance between the two groups, you can do like this (there may be a cleaner way to do that but that's the only one I can think of from the top of my head):
mydata <- data.frame(a=c(1:5),b=c(6:10),c=c(11:15),d=c(16:20))
plot <- stripchart(mydata, method="jitter", vertical=T, main='plot',pch=19,
at=c(1,2,4,5),xlim=c(0,6))
mtext(c('a+b','c+d'),1,line=3,at=c(1.5,4.5))
Argument at of stripchart is the one to fiddle with, but you then have to modify the plot limits (xlim) and the x-value at which you write the axis label (in mtext).
I'm plotting a graph using this
plot(dates,returns)
I would like to have the returns expressed as percentages instead of numbers. 0.1 would become 10%. Also, the numbers on the y-axis appear tilted 90 degrees on the left. Is it possible to make them appear horizontally?
Here is one way using las=TRUE to turn the labels on the y-axis and axis() for the new y-axis with adjusted labels.
dates <- 1:10
returns <- runif(10)
plot(dates, returns, yaxt="n")
axis(2, at=pretty(returns), lab=pretty(returns) * 100, las=TRUE)
If you use ggplot you can use the scales package.
library(scales)
plot + scale_y_continuous(labels = percent)
library(scales)
dates <- 1:100
returns <- runif(100)
yticks_val <- pretty_breaks(n=5)(returns)
plot(dates, returns, yaxt="n")
axis(2, at=yticks_val, lab=percent(yticks_val))
Highlights:
No need to explicitly add "%"
Manually fix the number of y-ticks to be consistent with further plots. Here I chose 5.
Combining two answers together #rengis #vladiim
How to plot the density of a single column dataset as dots? For example
x <- c(1:40)
On the same plot using the same scale of the x-axis and y-axis, how to add another data set as line format which represent the density of another data that represents the equation of
y = exp(-x)
to the plot?
The equation is corrected to be y = exp(-x).
So, by doing plot(density(x)) or plot(density(y)), I got two separated figures. How to add them in the same axis and using dots for x, smoothed line for y?
You can add a line to a plot with the lines() function. Your code, modified to do what you asked for, is the following:
x <- 1:40
y <- exp(-x)
plot(density(x), type = "p")
lines(density(y))
Note that we specified the plot to give us points with the type parameter and then added the density curve for y with lines. The help pages for ?plot, ?par, ?lines would be some insightful reading. Also, check out the R Graph Gallery to view some more sophisticated graphs that generally have the source code attached to them.