I am trying to plot two line curves on one chart using plot() and then lines().
But with this the second line curve goes out of chart and is half plotted.
plot(A, main="Bot",xlab="Days",ylab="$$$",type='l',col = 'Green')
lines(B, main="Bot",col = 'Red')
Am I missing any parameter for the scale?
Compute the minimum value for y on the red curve, then add the parameter ylim=c(MinY, 4000) into your first plot.
Related
I want to plot a roc curve in R but with legends outside the plot.
I want the axes labels to be from 0-1. So I used pty="s" but I am trying to fit the legend its not working.
par(pty="s",xpd=T, mar=par()$mar+c(0,0,0,3))
plot(g1,main="BPH vs G6", lty=1,xlab="Specificity", ylab="Sensitivity",
lwd=2,cex.main=2,font.axis=2,font.lab=2,cex.lab=2,cex.axis=1.2)
lines(g2, lty=1, col="red", lwd=2)
legend(-0.1,0.8,
legend=c("301+CN","CN"),
lty=c(1,1),col=c("red", "black"),cex=0.5)
I do not want the diagonal line to go outside the plot. Also, I want to increase the legend size, but by adjusting cex its not fitting here.
I am using plot3d() from rgl to make 3D scatter plots, using three columns, of samples in a data frame. Furthermore, I am using a fourth column colorby from my data frame (where each sample takes values, say, 10, 11, 12 as factors/levels) to color the points in the plot.
When using plot() to make 2D plots, I first set palette(rainbow(3)) and then col = colorby within plot() followed by legend(). The plot, colors and legend work fine.
However, when I repeat the last part for plot3d(), the coloring mixes up and not the same colors are assigned to the same levels as they would be in plot(). Moreover, if I use legend3d("topright", legend = levels(colorby), col = rainbow(3)) to create a legend, it looks the same as the 2D legend, but the coloring is clearly wrong in the 3D plot.
Where am I going wrong?
This looks correct to me:
df <- data.frame(x=1:9, y=9:1, z=101:109, colorby=gl(3,3))
palette(rainbow(3))
plot(x~y, df, col = df$colorby)
library(rgl)
with(df, plot3d(x,y,z, col = colorby))
legend3d("topright", legend = levels(df$colorby), col = levels(df$colorby), pch=19)
I was trying to plot a climate diagram and ran into the following problem:
After using barplot(...) for precipitation I superimposed another plot for the temperature. It is necessary for climate diagrams that the two y-axes (mm, °C) align at zero and that the precipitation/temperature ratio is 2:1 (e.g. 20mm precipitation corresponds to 10°C).
The problem: barplot(...) draws the axis to the plot's box while plot(...) leaves some space between the box and the axis margins.
Here is a simplified example. From the grid lines you see that the 0-values do not align:
barplot(0:10)
grid(col=1)
par(new=TRUE)
plot(0:10, xlim=c(-2,14), axes=FALSE)
axis(4,at=c(0:10), labels=c(0:10))
How can I get the right position and scaling of the two axes?
Don't use par(new = TRUE):
barplot(0:10)
grid(col=1)
lines(0:10, type = "p")
axis(4,at = c(0:10), labels = seq(0,20,2))
The function lines() is the right one here. The argument type = p is needed to plot points.
You need to adjust the y-values for the temperature, but now the second y-axis is in the right way, I think.
is there a way to have a curve with two colors, well i have many curves in my plot. But I like to add a specific characteristic, i want the curve 1 to be simple line in[a,b] interval, and dotted line in interval [b,c].
an example of my graph:
plot exp(-x**2 / 2), sin(x)
can we make sin(x) plotted in dotted line from[0,5]
thanks in advance.
You can specify the line type on a certain interval with the linetype keyword to the plot command. I don't think there is a way to make linetype a function of interval without doing it manually. For example:
plot exp(-x**2/2), [0:5] sin(x) lt 1 lc rgb 'green', [5:] sin(x) lt 2 lc rgb 'green'
gives
I'm using prcomp to do PCA analysis in R, I want to plot my PC1 vs PC2 with different color text labels for each of the two categories,
I do the plot with:
plot(pca$x, main = "PC1 Vs PC2", xlim=c(-120,+120), ylim = c(-70,50))
then to draw in all the text with the different colors I've tried:
text(pca$x[,1][1:18], pca$[,1][1:18], labels=rownames(cava), col="green",
adj=c(0.3,-0.5))
text(pca$x[,1][19:35], pca$[,1][19:35], labels=rownames(cava), col="red",
adj=c(0.3,-0.5))
But R seams to plot 2 numbers over each other instead of one, the pcs$x[,1][1:18] plots the correct points I know because if I use that plot the points it works and produces the same plot as plot(pca$x).
It would be great if any could help to plot the labels for the two categories or
even plot the points different color to make it easy to differentiate between the plots easily.
You need to specify your x and y coordinates a bit differently:
text(pca$x[1:18,1], pca$x[1:18,2] ...)
This means take the first 18 rows and the first column (which is PC1) for the x coord, etc.
I'm surprised what you did doesn't throw an error.
If you want the points themselves colored, you can do it this way:
plot(pca$x, main = "PC1 Vs PC2", col = c(rep("green", 18), rep("red", 18)))