This question already has answers here:
Plotting multiple curves same graph and same scale
(5 answers)
Closed 4 years ago.
I spent a long time trying to figure something out which I thought would be very easy. I have three vectors (or a data frame if you want to make it into one)
date <- c("Q1","Q2","Q3","Q4")
group1 <- c(12,13,16,11)
group2 <- c(9,11,10,9)
Now I want to create one graph with the date along the x-axis, and two horizontal lines representing the 2 groups. For a bit of context, I did a difference-in-difference regression and want to show the average values for treatment and control group around the event. I'm using panel data and already calculated the mean for both groups at each point in time. Here is a sceenshot I took from my so you can see how I want it to look like.
# plot solid line, set plot size, but omit axes
plot(x=seq(date), y=group1, type="l", lty=1, ylim=c(5,20),
axes=F, bty="n", xaxs="i", yaxs="i", main="My Title",
xlab="", ylab="Total Risk-Based Capital Ratio")
# plot dashed line
lines(x=seq(date), y=group2, lty=2)
# add axes
axis(side=1, labels=date, at=seq(date))
axis(side=2, at=seq(5,20,3), las=1)
# add vertical red line
abline(v=2, col="red")
# add legend
par(xpd=TRUE)
legend(x=1.5, y=2, legend=c("solid", "dashed"), lty=1:2, box.lty=0, ncol=2)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a data frame and I would like to plot as show the Figure 1.
My data frame has three column.
Each line must be plot on the same X-axis (three values by X-axis, as show the Figure 1).
It has a line that passing between the middle values (in this case, line [1, V2], [2, V2], [3, V2]... [n, V2].
Figure 1.
Figure 1 is only an example. I know the values are wrong in the plot.
I echo #divibisan's comment that if you just quickly want a plot with error bars, then you should take a look at ggplot2::geom_errorbar.
However, if you want fine control over each aspect of the plotting surface, but at the expense of a lot more typing, then here's one way to proceed:
# example data
randos <- runif(5, 0, 1)
df <- data.frame(
v1 = randos,
v2 = randos+1,
v3 = randos+2
)
# create empty plot
plot(x=1:nrow(df), y=1:nrow(df), pch=NA, # plot some data but don't show it
ylim=c(0, ceiling(max(df)+2)), # adjust y axis limit
xaxt="n", yaxt="n", # remove axes
bty="n", # remove box around plot
xlab="", ylab="", main="") # label axes and title
# add vertical lines
for(i in 1:nrow(df)) {
points(x=c(i,i), y=c(df$v1[i], df$v3[i]), type="l")
}
# add horizontal lines
points(x=1:nrow(df), y=df$v2, type="l")
# add points
points(x=1:nrow(df), y=df$v1, pch=15, col="forestgreen")
points(x=1:nrow(df), y=df$v2, pch=19, col="forestgreen")
points(x=1:nrow(df), y=df$v3, pch=8, col="forestgreen")
# add back axes
axis(side=1, 1:nrow(df))
axis(side=2, 0:(ceiling(max(df))+2))
# an example of how to add text
text(x=1:nrow(df), y=df$v3, labels=format(df$v3,digits=2), pos=3)
This question already has an answer here:
R - Customizing X Axis Values in Histogram
(1 answer)
Closed 7 years ago.
I'm learning R and I'm trying out the hist() histogram function. My code is here(and pasted below), and when I run it, the axes A) Do not connect at the origin, and B) they don't extend far enough for the dataset. I've looked and haven't found anything, the properties xlim, ylim, axes=FALSE, none of these solutions work.
bluegill = read.table(file="lab2.csv", header="true", sep=",")
attach(bluegill)
hist(Length, main="", xlab="Length (mm)", ylab="Number of individuals", col="gray")
and then this is the resulting chart, the max length is 220 in the data set, and the x axis only goes to 200.
A simple solution could be this:
bluegill = read.table(file="lab2.csv", header="true", sep=",")
attach(bluegill)
hist(Length, main="", xlab="Length (mm)", ylab="Number of individuals", col="gray", xaxt = "n") ##no x axis
Please notice the new option that has been added, xaxt = "n", which removes the x axis completely. You could then add the x axis late with another command. e.g.
axis(1, at = seq(0, 200, 20))
The first option is 1, which means x axis.
The second option stands for the points that will be shown in the plot (excuse my English).
I'm new to R so forgive me if the problem is obvious.
The problem I've got is that my legend just wont get shown
and there're no errors telling me something is wrong. I've been checking
the documentations for legends and I can't see where I've gone wrong.
The legend code looks like this:
legend(1, g_range[2], c("Client0","Client1"), cex=0.8,
col=c("blue","red"), pch=21:22, lty=1:2);
A note, everything in my graph works, just that the legend isn't drawn!
How is the drawingorder in R? What happens if one of the plots / lines crosses the space where the legend is supposed to be?
Will the line/plot be seen through the legend, will the legend overlap the lines/plot, will the plot/lines "hide" the legend?
Here is the whole code:
#Define the two Clients and their Deficencies
Client0 <- c(0,3.14,3.60,3.41,3.7,3.6,3.6)
Client1 <- c(0,1.26,1.7,4,2.82,3.42,4)
# Calculate range from 0 to max value of cars and trucks
g_range <- range(0, Client0, Client1)
# Graph autos using y axis that ranges from 0 to max
# value in cars or trucks vector. Turn off axes and
# annotations (axis labels) so we can specify them ourself
plot(Client0, type="o", col="blue", ylim=g_range,
axes=FALSE, ann=FALSE)
# Make x axis using Mon-Fri labels
axis(1, at=1:7, lab=c("1","2","3","4","5","6","7"))
# Make y axis with horizontal labels that display ticks at
# every 1 marks. 1*0:g_range[2] is equivalent to c(0,1,2,3).
axis(2, las=1, at=1*0:g_range[2])
# Create box around plot
box()
# Graph trucks with red dashed line and square points
lines(Client1, type="o", pch=22, lty=2, col="red")
# Create a title with a red, bold/italic font
title(main="Deficiencies", col.main="red", font.main=4)
# Label the x and y axes with dark green text
title(xlab="Time", col.lab=rgb(0,0.5,0))
title(ylab="Deficiencies", col.lab=rgb(0,0.5,0))
# Create a legend at (1, g_range[2]) that is slightly smaller
# (cex) and uses the same line colors and points used by
# the actual plots
legend(1, g_range[2]-1, c("Client0","Client1"), cex=0.8,
col=c("blue","red"), pch=21:22, lty=1:2);
I either seem to be running in circles or there is something wrong with my data. I want to plot some data and use ?axisto modify the labels on the X axis.
I have two issues however:
The axis labels begin in the center of the x-axis instead of at the beginning
The axis labels do not match the data points in the plot
I would like to have X axis labels ranging from 10 to 90 by 5.
This is the code that I use and came up with so far:
values <- cbind(1:180,1)
l <- list(1:10,11:20,21:30,31:40,41:50,51:60,61:70,71:80,81:90,91:100,101:110,111:120,121:130,131:140,141:150,151:160,161:170,171:180)
# compute mean across the intervals in l
meanqual <- sapply(l, function(x) mean(values[x,1]))
meanqual
plot <- plot(meanqual, type="o", xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5))
If you only give one numeric vector to plot it "assumes" you meant to use the position or index of the values in that vector as the x values, so the plot call plotted meanqual against 1:length(meanqual). If you wanted to plot against the seq() argument you latter used in the axis call you should supply it (or rather something similar in scale with the same length as meanqual) to plot:
plot <- plot(x=seq(5,90,5), y=meanqual, type="o",
xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5), labels=seq(10,90,5))
This question already has answers here:
Remove plot axis values
(5 answers)
Closed 6 years ago.
I want to create a plot with ticks and labels above and to the right of the plot using axis(). How do I suppress the ticks and labels that 'automatically' print with the plot() function? Thank you
x<-1:10
y<-1:10
quartz("test")
par(mar=c(10,10,10,10)+0.1)#sets margins of plotting area
par(pty="s")#fixes the aspect ratio to 1:1
#Automatically adds ticks, numbers and axis labels. How can I avoid this?
plot(x,y,typ="n")
#Adds axes above and to the right of plot area...I want these only
axis(side=4,las=2, ylab="y label")
axis(side=3,las=1,xlab="x label")
See ?plot.default:
plot(x, y, type="n", axes=FALSE, frame.plot=TRUE, xlab="", ylab="")