Dot Plot in R using R Plotting - r

What is the best way to generate a dotplot with two factors like this, preferably using standard R plots (not ggplot) and from a 2x2 data frame. The horizontal lines should be means. I have tried cleveland dot charts, but cannot figure out how to get two data series' and have the dots jittered:

The following piece of code should do the trick:
set.seed(1)
t1 = rnorm(10); t2 = rnorm(10, 2)
t1_g2 = rnorm(10, 4);t2_g2 = rnorm(10)
##Don't print the axes labels
par(ann=FALSE)
##Plot first set of data.
##Need to check for sensible ranges
##Use the jitter function to spread data out.
plot(jitter(rep(0,10),amount=0.2), t1,
xlim=range(-0.5,3.5), ylim=range(-3,8),
axes=FALSE,frame.plot=TRUE)
points(jitter(rep(1,10), amount=0.2), t1_g2, col=2)
points(jitter(rep(2,10), amount=0.2), t2)
points(jitter(rep(3,10), amount=0.2), t2_g2, col=2)
##Add in the y-axis
axis(2, seq(-4,8,by=2))
##Add in the x-axis labels
mtext("Treatment 1", side = 1, at=0.5)
mtext("Treatment 2", side = 1, at=2.5)
##Add in the means
segments(-0.25, mean(t1), 0.25, mean(t1))
segments(0.75, mean(t1_g2), 1.25, mean(t1_g2))
segments(1.75, mean(t2), 2.25, mean(t2))
segments(2.75, mean(t2_g2), 3.25, mean(t2_g2))
##Add in the legend
legend(0, 8, c("Group 1", "Group 2"), col=1:2, pch=1)
which gives:

Related

Scatterplot numerical y against two x groups

From my data, I created the following vectors:
ratio_automatic <- c(2.031, 2.24, 2.00, 0.46, 2.75, 0.86, 2.69, 0.44)
ratio_manual <- c(1.02, 2.40, 1.53, 0.50, 1.38, 0.70, 1.69, 0.54)
method <- rep(c("Manual", "Automatic"), each = 8)
to create the following dataframe:
df <- data.frame(method, c(ratio_automatic, ratio_manual))
names(df) <- c("method", "ratio")
So I think that is then called narrow data?
In summary: there are 16 observations to two variables.
I want to plot the data in a scatterplot, so you can see the ratio datapoints grouped by category (either 'manual' or 'automatic').
When I run the following code:
plot(1:nrow(df), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:16, labels=df$method)
I get the following graph:
This is not what I want, as I want on the x-axis just 'automatic' and 'manual' and then the corresponding points above that, not each point separate like now.
is there a way to fix this?
(I'm still very new to R, so I'm sorry if this is an obvious question.)
If you want "Automatic" and "Manual" to appear only once on the x-axis, then you shouldn't make a scatterplot. Scatterplots assumes that both your x- and y-axes are continuous. Consider making a boxplot and overlaying with jittered points, similar to what you would find in a scatterplot.
# make a boxplot
boxplot(df$ratio ~ df$method, col = "white")
# add some points
stripchart(df$ratio ~ df$method,
method = "jitter",
pch = 19,
vertical = TRUE,
add = TRUE)
Maybe you want something like this where you start with an empty plot and then plot on the 1 and 2 of your factor methods and use points to add the data like this which results in a jitter plot ( you can adjust the positions of your labels in axis):
plot(1, type="n", xlim=c(0, 3), xaxt="n", ylim = c(0, 3), xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:2, labels=unique(df$method))
points(factor(df$method), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)
Output:

Combine integrated R plots with ggplot

I am trying to make a layout plot that merges 3 plots. I want the first plot in the first row and the other ones in the second row like this image:
The problem comes when I try to combine plots integrated in R and a plot made with ggplot2 library.
My code is the following:
layout(matrix(c(1, 1,
2, 3), nrow=2, byrow=TRUE))
layout.show(n=3)
qplot(area_mean,
geom="histogram",
binwidth = 5,
main = "ggplot area min histogram",
xlab = "Age",
fill=I("blue"),
col=I("red"),
alpha=I(.2),
xlim=c(0,3000))
hist(area_mean, col="purple", main="Histogram of area mean")
boxplot(area_mean, col="cyan", main="Boxplot of area mean")
Then my output image is:
Why qplot doesn't appear? I am missing something?
Thank you!!

How to place multiple plots on top of each other (or along the x-axis) in R

I'm looking to make a graph in R, something like this
Where plot 1, 2, and 3 are are at specified markings along the x-axis.
Noting an answer to a similar question, I think this can be done using the grconvertX function with fig, but I can't seem to get the code I wrote to plot more than one of the subplots. Here is the toy data and code.
#example
#main plot variables
x = seq(0,1, by=.1)
y = (x/(.2+x))
plot(x,y, type='b')
#subplot variables
xlocations = c(.2,.6,.8)
nXticks = length(xlocations)
for(i in 1:nXtick){
xloc = xlocations[i]
xRad = .05
par(fig = c(grconvertX(c(xloc-xRad, xloc+xRad), from="user", to="ndc"),
grconvertY(c(0, 0.1), from="user", to="ndc")),
mar = c(0,0,0,0),
new = TRUE)
plot(1:10,1:10, axes=F, xlab='', ylab=''); box();
}

Plot average of values in single plot

I want to plot single bar in a graph so it would look like picture below
I created test data and calculate its mean.
value <- c(99,44,100,120,88)
value_mean <- mean(value)
And plot them using below code
barplot(value_mean, horiz=T, width=30, cex.names=0.5 ,ylim=c(0,200), col="red")
Buth the output is not even close.
I've also looked at this links
Single bar barchart in ggplot2, R
R Barplot with one bar - how to plot correctly
So my output should be something like the first picture. I was thinking that could solve ggplot.
If everything else fails, you can draw a rectangle like this:
par(mar = c(12, 2, 12, 2))
plot(0, type="n", ylim=c(-1, 1), xlim=c(0, 200), axes = F, bty = "n",ylab="", xlab="label")
rect(0, -.7, value_mean, .7, col="red", border=NA)
text(value_mean, 0, pos=4, label=value_mean)
axis(1, at=seq(0, 200, by=40))

misplaced label on scatter plot data

I am quite new to R and was wondering if anyone could help with this problem:
I am trying to graph a set of data. I use plot to plot the scatter data and use text to add labels to the values. However the last label is misplaced on the graph and I can't figure out why. Below is the code:
#specify the dataset
x<-c(1:10)
#find p: the percentile of each data in the dataset
y=quantile(x, probs=seq(0,1,0.1), na.rm=FALSE, type=5)
#print the values of p
y
#plot p against x
plot(y, tck=0.02, main="Percentile Graph of Dataset D", xlab="Data of the dataset", ylab="Percentile", xlim=c(0, 11), ylim=c(0, 11), pch=10, seq(1, 11, 1), col="blue", las=1, cex.lab=0.9, cex.axis=0.9, cex.main=0.9)
#change the x-axis scale
axis(1, seq(1, 11, 1), tck=0.02)
#draw disconnected line segments
abline(h = 1:11, v = 1:11, col = "#EDEDED")
#Add data labels to the graph
text(y, x, labels= (y), cex=0.6, pos=1, col="red")
Your probs request returns 11 values, but you only have 10 x values. Therefore R recycles your y values, and the 11th label is plotted at y = 1 when you add the text. How to fix this depends upon what you are trying to do. Perhaps in your probs sequence you want seq(0, 1, length.out = 10)?

Resources