make a linestring and then plot it with ggplot2 but customize the line width to be thicker in r

To create a linestring and plot it with ggplot2 in R, you can use the following code:

main.r
# Install and load necessary libraries
install.packages(c("ggplot2", "sf"))
library(ggplot2)
library(sf)

# Create a linestring
linestring <- st_linestring(list(rbind(c(0, 0), c(1, 1), c(2, 2))))

# Create a simple feature collection
sfc <- st_sfc(linestring)

# Create a data frame
df <- data.frame(geometry = sfc)

# Plot the linestring with a thicker line
ggplot(df) + 
  geom_sf(size = 2) + 
  theme_void()
411 chars
19 lines

In this code:

  • We first install and load the necessary libraries (ggplot2 and sf).
  • We then create a linestring using st_linestring.
  • We create a simple feature collection (sfc) and add the linestring to it.
  • We create a data frame with the geometry column containing the linestring.
  • We plot the linestring using ggplot and geom_sf, customizing the line width by setting size = 2.
  • Finally, we use theme_void to remove the background and other unnecessary elements.

This will create a simple plot with a thicker linestring. You can customize the appearance further by using other geoms and theme elements provided by ggplot2.

related categories

gistlibby LogSnag