Background:
I have a polygon() that doesn't look as professional as I need it to be (see my R code blow).
I usually use two general techniques to make ploygon()s come out the way I want them to be. First, in my curve, I use lwd = larger than 1. Second, in my polygon, I remove the border of my polygon() by border = NA.
In this case below, for some reasons, I can NOT use the first technique.
Question:
I was wondering what else I can do in addition to removing the border=NA in my polygon() to improve my polygon?
curve(dnorm(x), -3, 3, bty="n", ann=F, axes=F, col="blue")
xs <- seq(-0.5, 0.5, len=1000)
polygon(c(xs[1], xs, xs[1000]), c(0, dnorm(xs), 0), col='grey', border = NA)
I don't know how much better this is, but maybe add curve after the polygon
curve(dnorm(x), -3, 3, bty="n", ann=F, axes=F, col=NA)
xs <- seq(-0.5, 0.5, len=1000)
polygon(c(xs[1], xs, xs[1000]), c(0, dnorm(xs), 0), col='grey', border = NA)
curve(dnorm(x), -3, 3, col='blue', add = TRUE, n = 1e4)
#n = 1e4 helps plot curve with more points (default in 101), making it smoother
Related
I am relatively new to R. I am making an R Shiny app, and based on the input of the user, I would like to analyze the data and output a bar that shows the Jaccard index. This is what I want it to look like, although obviously a smooth gradient:
Please note that the Jaccard index (in this case, 0.35) will change after each input, so I'd like something reactive. I just have no idea where to start or if making plots like this is even possible in R.
Thanks.
edit: I used an online gradient generator to come up with this plot instead: how could I overlay a vertical line with its corresponding Jaccard index and corresponding location on the bar on this particular image?
edit: I want to remove the white space before the actual plot and after my text. any ideas?
With the plotrix package:
library(plotrix)
# get an empty box
plot(0:10, type="n", axes=FALSE, xlab=NA, ylab=NA)
# rectangle filled with a gradient
gradient.rect(0, 0, 10, 5, col=smoothColors("red",38,"blue"), border=NA)
# vertical bar
segments(3.5, 0, 3.5, 5, lwd=2)
text(3.5, 0, "0.35", pos=1, xpd=TRUE)
For something in base R, an imperfectly modified version of this solution, might work.
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut)-1)/(max-min)
dev.new(width=1.75, height=5)
plot(c(min,max), c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
for (i in 1:(length(lut)-3)) {
x = (i-1)/scale + min
rect(x, 0 ,1, 30/scale, col=lut[i], border=NA)
}
}
Then make the graph as follows -
color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), 0, 1)
At this point perhaps you can add an abline(v = 0.35) to get what you want?
You could even try pointing to the appropriate position using an arrow using
arrows(0.35, -1, 0.35, 0, length = 0.07, angle = 25)
I feel like this is a very basic question but I have spent a lot of time looking for an answer and haven't found one. So, if this is answered somewhere else I would love to be redirected rather than downvoted, please.
Anyway, my problem is that when I graph in R, often the y-axis will fail to extend to the end of my data. A sample graphic is below, where you can see that it would be better for the axis to go all the way to 30 rather than 20. However, submitting ylim = c(0,30) doesn't do anything and I cannot think of or find another command that would do the trick?
Here is a reproducible example. If ylim usually works then I am assuming something is breaking because of the aesthetic changes I've made?
set.seed(1)
x<-runif(1:1000, min=1, max=10)
hist(x, breaks=100, main=NA, axes=F, xlab = NA, ylab = NA)
axis(side = 1, tck= -.01, labels=NA)
axis(side = 2, tck=-.01, labels=NA)
axis(side = 1, lwd=0, line= -.4, cex.axis=1.4)
axis(side = 2, lwd=0, line=-.4, las=1, cex.axis=1.4)
mtext(side = 1, "Percent pathogenic bacteria", line = 2.5, cex=1.8)
mtext(side = 2, "Frequency", line = 2.5, cex=1.8)
Use ylim to specify the y axis range:
set.seed(3)
f <- function(y, ...)
hist(y, breaks=20, ...)
ylim <- range(pretty(ceiling(f(y <- rchisq(1000, 3), plot=FALSE)$counts/10)*10))
f(y, ylim=ylim) # versus f(y)
I can get photo11 with the following code,how can i fix my code to change photo1 into photo2?
x = seq(0.5, 0.9, length = 200)
y = dnorm(x,0.7,0.0458)
plot(x, y,type="l",xlab="my_x_lab")
this is a photo1.jpg
this is a photo2.jpg
and ,how to fix my code to change photo2.jpg into photo3.jpg?there are
only two scales(0.7,0.8) in the x_lab.
this is a photo3.jpg
to fix the code to get rid of y in the y_lab to change photo3.jpg into photo4.jpg?
this is a photo4.jpg.
This will do it. xaxt='n', ann=FALSE removes the x-axis and annotations. axis(...) puts the x-axis only at the specified points. mtext() will put marginal text on the bottom axis.
x <- seq(0.5, 0.9, length = 200)
y <- dnorm(x,0.7,0.0458)
plot(x, y, type="l", xaxt='n', ann=FALSE)
axis(1, at=c(0.7, 0.8))
mtext("my_x_lab", 1, at=0.9, line=2)
Suppress the x axis and add blanks for the labels where you do not want them.
plot(x, y, type="l", yaxt="n",ann=FALSE,bty="n", xaxt="n")
axis(1, at=c(0.5, 0.6, 0.7, 0.8, 0.9), labels=c("", "", 0.7, 0.8, 0.9) )
mtext("Proportions", 1, at=0.9, line=2)
If you insist on omitting the ticks in the left hand side its going to be more difficult because the base line will only extend from the first tick.
Except this answer is better than mine.
how to draw the graph in R?
I guess it means this is homework? Oh NOOOO, it's the same poster... you are posting duplicate questions? Bad poster , bad poster. Shame on you.
I am trying to make a plot in R that has a portion of the plot grey to emphasize this area. Unlike other examples, I don't want to color an area under a plot, but instead color an area on a plot starting at one area and going to the end of the graph. When I try to use rect() or polygon() it obscures the plots I want to emphasize.
For example:
x_mean <- c(1, 2, 3, 4)
y_mean <- c(1, 1, 1, 1)
y_max <- c(4, 4, 4, 4)
y_min <- c(-4, -4, -4, -4)
x_shade <- c(2, 3, 4)
y_max_shade <- c(4, 4, 4)
y_min_shade <- c(-4, -4, -4)
plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" )
arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0)
points( x=x_mean, y=y_mean, pch=16)
This will plot 4 lines on the graph. How do I draw a grey box in the background from the 2nd line to the end of the plot?
Just so that you're left with more than just a comment, here's a possible solution:
plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" )
rect(2,-4,4,4,col = rgb(0.5,0.5,0.5,1/4))
arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0)
points( x=x_mean, y=y_mean, pch=16)
Note that I also demonstrated how to use alpha blending in the color specification (using rgb). This can also be useful for this sort of thing. Try moving the rect line to the end, and notice that the results still look ok, because the fill color is partially transparent.
I've found this answer to be pretty great for shading background parts of R.
Some context:
panel.first = rect(c(1,7), -1e6, c(3,10), 1e6, col='green', border=NA)
The first two arguments c(1,7) are the starting values for the shaded rectangle, and following arguments c(3,10) are where the shading ends. This creates a shaded region from 1-3 and 7-10.
If you take the code below, how can you change the filling of the third diamond, so it will be half-black, half-white? The solution should apply with any colors.
data <- c(1,2,3)
plot(data, pch=c(23,18,23), cex=c(2.5,3,2.5))
The pch characters are actual font symbols, so you will be limited to what is available in your fonts. Another alternative is to use the primitive plotting commands to make your own "symbols". This is very flexible, although there can be issues with resizing, etc., when mixing the two methods. This method is implemented in the nice my.symbols() example:
require(TeachingDemos)
bwDiamond <- function() {
plot.new()
polygon(c(0, 0.5, 1, 0.5, 0), c(0.5, 0, 0.5, 1, 0.5), lty=1)
polygon(c(0.25, 0.5, 1, 0.75, 0.25), c(0.25, 0, 0.5, 0.75, 0.25), col=1)
}
data <- c(1,2,3)
dev.new(width=4, height=4)
plot(data, type='n')
points(data[1:2], pch=c(23,18), cex=c(2.5,3))
my.symbols(data[3], data[3], symb=bwDiamond, symb.plots=TRUE, inches=0.22)
See also this Q/A: Point symbols in R
#JohnColby : Ok, here is a way to draw a half-coloured circle point, using grid.semicircle from lodplot package. It's not such tidy solution as yours with polygons, but it does the job:
require(lodplot)
dev.new(width=4,height=4)
plot(1:3,type="n")
grid.semicircle(100,150,10,5)#left circle on plot below
grid.semicircle(100,150,10,5,side=3,col=1)
grid.semicircle(200,150,10,5,side=2)#right circle on plot below
grid.semicircle(200,150,10,5,side=4,col=1)