In: Computer Science
Hey anyone good with R studio?
How do you create a square shape with R code by using plot() and lines()
please show your code !
In this example, we are going to draw a simple square polygon to an empty R plot. Let’s first create an empty plot:
plot(1, 1, col = "white", xlab = "X", ylab = "Y") # Draw empty plot
To this plot, we can draw a polygon with the following R code:
polygon(x = c(0.7, 1.3, 1.2, 0.8), # X-Coordinates of polygon
y = c(0.6, 0.8, 1.4, 1), # Y-Coordinates of polygon
col = "#1b98e0") # Color of polygon
It will look like this
As you can see, the previous polygon() R code consists of three different components:
For the second example, I’m using exactly the same code as in Example 1, but this time I’m modifying the border color of the polygon. Let’s begin again with an empty graph…
plot(1, 1, col = "white", xlab = "X", ylab = "Y") # Draw empty plot
…and then let’s draw a thick red border around the polygon:
polygon(x = c(0.7, 1.3, 1.2, 0.8), # X-Coordinates of polygon
y = c(0.6, 0.8, 1.4, 1), # Y-Coordinates of polygon
col = "#1b98e0", # Color of polygon
border = "red", # Color of polygon border
lwd = 5) # Thickness of border
We used two further options within the R polygon function:
That’s it with the square polygon examples.