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)
Related
I want to generate canonical correspondence analysis(CCA) plot, to show the influence of environmental parameters on species distribution using this code:
spe <- read.csv("spe.csv", row.names=1, sep=";")
env <- read.csv("env.csv", row.names=1, sep=";")
ccamodel <- cca(spe~., env)
plot(ccamodel, xlim=c(-1.5,2), ylim=c(-1,1.5), display=c("sp","cn"))
Here is the output generated where the environmental variables and species name are highlighted in blue and red respectively.
My questions: How I can put the same symbol as a filled circle or a filled square before or after each species name in the plot?
Thanks in advance
I cannot quite match your plot. The scaling of the biplot is different but otherwise this accomplishes what you want.
out <- plot(ccamodel, type="n", xlim=c(-2.5, 2.75), ylim=c(-1, 1.5))
text(ccamodel, dis="bp", scaling="species", col="blue", cex=.8)
points(ccamodel, pch=16, scaling="species", display="sp", col="red")
text(ccamodel, dis="species", scaling="species", col="red", cex=.8, pos=4)
The text is shifted to the right of the points for clarity. The pos= argument sets the position. If you leave it out the text is centered on top of the point, values of 1 - 4 put it below, left, over, and right of the point.
This question already has an answer here:
Ordering of points in R lines plot
(1 answer)
Closed 3 years ago.
I used plot(x, y, type="p") to draw a scatter plot, and it seems right (Figure 1). However, when using plot(x, y, type="l") to draw a line, there are some mussy lines (Figure 2). Why didn't it a "single" line?
Looks like your x vector needs to be sorted, when using line plots, the order in which your points are submitted is very important as the lines are drawn connecting one point to the next one.
y <- y[order(x)]
x <- x[order(x)]
# now you can make your plot
plot(x, y, type="l")
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
Using R's base plots, I know well how to control the margins between R plots and the outer margins of a panel of plots using, respectively:
par(mar=c(1,1,0,0))
par(oma=c(1,1,0,0))
What I am trying to learn is a general way to control the "inner margins" of a plot, which means: how can I increase/decrease the space between the axes and the plot content?
By "general way" I mean something that does not rely on simply manually adding/subtracting an offset to/from the min and max of the variable represented by a given axis. After all, such a solution could lead either to different padding between plots of a same plot panel depending on their scales and differences, or to the need of significant manual adjustments per plot within a plot panel.
Notice that my question is a direct analogous to this other one, but in my case I want to learn how to do it for base R plot instead of ggplot (it does have to be base plot).
As documented in ?plot.window, the default is to extend the range of your data by 4% and use those as the plot limits. You can't change the value 4% to another number, but you can set xlim and ylim explicitly to accomplish the same thing.
For example, if you want a p=50% extension instead, you want the lower limit for x to be min(x) - p*diff(range(x)), and a similar formula for the upper limit, and for y. But if you just use that as the first value in xlim, you'll 50%, followed by another 4%. You could work around this, but using xaxs = "i" will use the exact spec. Putting all of this together, here's an example:
x <- 1:10
y <- 5:14
p <- 0.5
plot(x, y, xlim = c(min(x) - p*diff(range(x)), max(x) + p*diff(range(x))),
xaxs = "i",
ylim = c(min(y) - p*diff(range(y)), max(y) + p*diff(range(y))),
yaxs = "i")
which produces this plot:
You can simply adjust the ranges of your x and y plot limits:
x <- 1:10
y <- 1:10
# Before
plot(x, y)
# After
plot(x, y, xlim = range(x) + c(-1, 1), ylim = range(y) + c(-1, 1))
Output:
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)
This question already has answers here:
Plotting half circles in R
(3 answers)
Closed 2 years ago.
I'd like to add points that are multicoloured dots, with one half in blue for ex. and one half in red. If possible, I'd like to be able to do it with more than 2 colors, for example 4 quarters, each with a color.
Is that at all possible?
I know I can superimpose several dots of different sizes, each with a color, resulting in a concentric multicolored dot. But this is not what I am after.
For context, I am using these points for cities in a map.
Here is a small example, with each city having a unicolored dot. But I would like each city to have a dot that has two halves, each one with a color.
require(ggmap);
citiesnames=c("Madrid","Toledo","Valencia","Granada")
cities=str_c(citiesnames,"Spain",sep=", ");
geo=geocode(cities);
lon=geo$lon;
lat=geo$lat;
coord=mapproject(lon, lat,proj="mercator");
map("world","Spain",fill=T,col="ivory",proj="mercator");
points(coord$x[1], coord$y[1], pch=16, cex=1.2, col="black")
points(coord$x[2],coord$y[2], pch=16, cex=1.2, col="red")
points(coord$x[3], coord$y[3], pch=16, cex=1.2, col="blue")
points(coord$x[4], coord$y[4], pch=16, cex=1.2, col="green4")
Any help greatly appreciated.
As I mentioned in the comments you can refer to this post for doing what you want. For documentation purposes I add an answer which resolve the issue:
You can add two half circles (pie charts) to your graph using these two functions:
upper.half.circle <- function(x,y,r,nsteps=100,...){
rs <- seq(0,pi,len=nsteps)
xc <- x+r*cos(rs)
yc <- y+r*sin(rs)
polygon(xc,yc,...)
}
lower.half.circle <- function(x,y,r,nsteps=100,...){
rs <- seq(0,pi,len=nsteps)
xc <- x-r*cos(rs)
yc <- y-r*sin(rs)
polygon(xc,yc,...)
}
Later when you are piloting you should set asp = 1 to get the half-circles.
plot(1, type="n",axes=F,xlab="", ylab="",xlim=c(0,200),ylim=c(0,200), asp = 1)
upper.half.circle(15,170,10,nsteps=1000,col='red')
lower.half.circle(15,170,10,nsteps=1000,col='blue')
Here's the result of this code:
You can later edit the fictions for getting quarters or whatever you need.