I'm trying to plot 100k points in a scatter plot. I couldn't distinguish between the points as the line width around the points is too big. Is there any way to remove this line or decrease the width of the line?
plot(x=A,
y=C,
Geom.point,
Theme(panel_fill=color("black"),
default_point_size=2pt,
default_color=color("red")));
You can use highlight_width=0pt (found here):
N = 10^4
A = rand(N)
B = rand(N)
plot(x = A,
y = B,
Geom.point,
Theme(panel_fill = colorant"black",
default_point_size = 2pt,
default_color = colorant"red",
highlight_width = 0pt)
)
Related
New to R. I'm given a function f(x)=x^3-3x+7. I need to plot this function in red and plot its derivative with a step-size of 0.5 in blue. dfx vs x in the same graph. I have plotted f(x), but i cant plot dfx with adjusted step size.
My code:
f1 <- function(x) {x^3-3*x+7}
exp = D(expression(x^3-3*x+7),'x')
f2 <- function(x) {D(exp,'x')}
curve(f1,from=-2,to=2,col='red')
curve(exp,col='blue',add=TRUE,type='p')
I need the points to be plotted at an interval of 0.5 and also draw a line to connect them
I'm not 100% sure what you need. If by changing the step size you mean that both curves be entirely visible in the same plot, that can be done by increasing the limits on the y-axis:
curve(f1, from = -2, to = 2, col='red', add = F, ylim = c(0,10))
curve(exp, col = 'blue', add = T, type = 'p')
I've added an verticle line at some point on x-axis (as shown in line plot of below Fig.) using abline() function in R, and I want to either put a legend for it or add some text over it (vertically on the abline).
So, is it possible to do this? If yes, then how I can do this?
Use text with the indicated arguments. Alternately use the legend function as shown.
abline is classic graphics so it is assumed you want to use that plotting system but if you want ggplot2 see Add a horizontal line to plot and legend in ggplot2 and make the obvious modifications to change the horizontal line to vertical.
x <- y <- 1:10; v <- 5 # input
plot(x, y)
abline(v = v, col = "red")
text("Vertical Line", x = v, y = max(y), srt = -90, pos = 4) # near top
text("Vertical Line", x = v, y = min(y), srt = 90, adj = c(0, -0.5)) # near bottom
legend("topleft", legend = "Vertical Line", pch = "|", col = "red")
I would like to plot two planes in a 3D plot. I have tried persp3d and it generates two planes. But instead of the whole two planes, I just want to show parts of them divided by the intersection line, i.e, "left" part of the blue plane, and "upper" part of the red plane. I tried xlim, ylim, but it seems my lims are not single values, but functions.
library(rgl)
x <- seq(-10, 10, length = 30)
y <- x
region = expand.grid(x=x, y=y)
z1 = region$x+2*region$y + 2
z2=3*region$x+region$y
persp3d(x,y,z1,col="steelblue")
persp3d(x,y,z2,col="red",add=TRUE)
grid = mesh(x,y)
z = with(grid,ifelse(x+2*y>3*x+y,x+2*y,3*x+y))
persp3D(z = z, x = x, y = y,col = NULL)
for (i in 1:900){
z[i] = ifelse(region$x[i]+2*region$y[i] + 2 >
3*region$x[i]+region$y[i],region$x[i]+2*region$y[i] + 2,3*region$x[i]+region$y[i])}
persp3d(x,y,z,col="steelblue")
This is inspired by Huang Rui's suggestion
I use the following script to generate a legend in R. But the legend box is too small... how do I increase the box width?
legend("topleft", lty = 1, legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),col = c("black","red","blue"))
You are probably resizing your graph after you plot it and the legend. If that is the case, and you want to keep the box, one option would be to plot the graph, resize it, and then generate the legend. Perhaps a better option would be to size the window to the desired width to start with:
# on Windows, you can use the `windows` function. elsewhere, try quartz or X11
windows(height = 7, width = 3.5)
plot(hp ~ mpg, data = mtcars)
leg <- legend("topleft", lty = 1,
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"),
#plot = FALSE,
#bty = "n")
)
You can also define exactly where you want the box to fall by providing a pair of x and y coordinates to the legend function. Those values would represent the upper left and bottom right corners of the box. The legend function will actually generate the coordinates for the upper-left hand corner of the box along with the width and height. By default it returns them invisibly, but you can assign them to an object, and If you use the plot = FALSE, option to legend you can capture those coordinates and modify them as you wish without actually plotting the legend.
windows(height = 7, width = 3.5)
plot(hp ~ mpg, data = mtcars)
legend(x = c(9.46, 31), y = c(346.32, 298),
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"),
lty = 1)
The legend function will actually generate the coordinates for the upper-left hand corner of the box (that's where I got 9.46 and 346.62) along with the width and height of the box. By default it returns them invisibly, but you can assign them to an object, and if you use the plot = FALSE, option to legend you can capture those coordinates and modify them as you wish without actually plotting the legend.
plot(hp ~ mpg, data = mtcars)
leg <- legend("topleft", lty = 1,
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"),
plot = FALSE)
# adjust as desired
leftx <- leg$rect$left
rightx <- (leg$rect$left + leg$rect$w) * 1.2
topy <- leg$rect$top
bottomy <- (leg$rect$top - leg$rect$h) * 1
# use the new coordinates to define custom
legend(x = c(leftx, rightx), y = c(topy, bottomy), lty = 1,
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"))
Part of the legend width is determined by the longest width of the labels you use, which is calculated via strwidth. Below an easy example how to halve or double the size by using legend(..., text.width = ...).
plot(1)
text = c("Sub_metering_1","Sub_metering_2","Sub_metering_3")
legend("topleft"
,lty = 1
,legend = text
,col = c("black","red","blue")
)
strwidth(text)
# [1] 0.1734099 0.1734099 0.1734099
# half the length
legend("bottomleft"
,lty = 1
,legend = text
,text.width = strwidth(text)[1]/2
,col = c("black","red","blue")
)
# double the length
legend("center"
,lty = 1
,legend = text
,text.width = strwidth(text)[1]*2
,col = c("black","red","blue")
)
Here is the problem:
x<-seq(0,10,length.out = 1000)
y1<-dnorm(x,mean = 2,sd=1)
y2<-dnorm(x,mean = 6,sd=1)
plot(x,y1,type="l")
lines(x,y2)
abline(v=x[380])
The graph is shown below. How can I fill 2 different colors, say red and blue, on the each side of vertical line but still below two normal density functions. I thought I can use polygon, but failed.
This is the graph without filling colors:
Here's one way:
First, we'll get the parallel minimum of your densities - this is a vector of the top y coordinates for our polygons.
y = pmin(y1, y2)
# set up your plot as in the question
plot(x, y1, type="l")
lines(x, y2)
# define a re-usable variable for the vertical line placement
x_vert = 380
abline(v = x[x_vert])
# Now we'll draw 2 polygons, one for the left side, one for the right.
# The first (x,y) pairs of the polygon are just the (x,y) coords of the
# density we're filling to, until the vertical line
# Then we need to connect the "bottom" points, which have coordinates
# (x[x_vert], 0) and (x[1], 0)
polygon(x = c(x[1:x_vert], x[x_vert], x[1]),
y = c(y[1:x_vert], 0, 0),
col = "blue")
# similar for the right hand polygon, but now going from x_vert to length(x)
polygon(x = c(x[x_vert:length(x)], x[length(x)], x[x_vert]),
y = c(y[x_vert:length(x)], 0, 0),
col = "red")
Voila!