When the data doesn’t show much variation, a line graph created with ggplot2
can look a bit plain. In such cases, adding points to each coordinate can make the graph clearer and more distinct. In ggplot2
, you can use the geom_point
function to add points. There are 26 different shapes you can choose from, not just circles. This allows for more effective visualization when dividing lines by groups, as you can set different point shapes for each group, as shown below.
Getting the sample data
I have prepared a sample dataset. Click the link below to download it.
file download : data.csv
Use the readr::read_csv
function to read the downloaded file and store it in a variable.
tb1 = read_csv("./data.csv")
# A tibble: 3 × 13
class `2021-11` `2021-12` `2022-01` `2022-02` `2022-03` `2022-04` `2022-05` `2022-06` `2022-07` `2022-08`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 work1 3032 3067 2619 2431 2641 3089 2904 2674 2943 2851
2 work2 2349 2238 2056 1754 2361 2320 2186 2160 2335 2340
3 work3 369 427 530 499 1131 1121 1270 1068 1351 1007
# ℹ 2 more variables: `2022-09` <dbl>, `2022-10` <dbl>
Since the data is in a pivot format, it needs to be transformed into a tabular format as shown below.
tb2 = tb1 %>%
inner_join(sb1, by = c("class" = "cd")) %>%
pivot_longer(cols = "2021-11":"2022-10") %>%
mutate(date = ymd(paste0(name, "-01")))
# A tibble: 36 × 5
class ord name value date
<chr> <dbl> <chr> <dbl> <date>
1 work1 2 2021-11 3032 2021-11-01
2 work1 2 2021-12 3067 2021-12-01
3 work1 2 2022-01 2619 2022-01-01
4 work1 2 2022-02 2431 2022-02-01
5 work1 2 2022-03 2641 2022-03-01
6 work1 2 2022-04 3089 2022-04-01
7 work1 2 2022-05 2904 2022-05-01
8 work1 2 2022-06 2674 2022-06-01
9 work1 2 2022-07 2943 2022-07-01
10 work1 2 2022-08 2851 2022-08-01
# ℹ 26 more rows
# ℹ Use `print(n = ...)` to see more rows
Creating a line graph with points
Once the data is prepared, you can easily create a line graph using the geom_line
function.
ggplot(tb2, aes(date, value, colour = class)) +
geom_line(size = 1)
To add points on top of the line graph, use the geom_point
function. You can adjust the size parameter to fit your needs.
ggplot(tb2, aes(date, value, colour = class)) +
geom_line(size = 1) +
geom_point(size = 4)
If you don’t set anything, the default shape for points is a circle, but you can change the shape by setting the shape
value. You can use numbers between 0 and 25 to specify different shapes. If setting numeric data is difficult, you can map shapes using a string field instead.
ggplot(tb2, aes(date, value, colour = class)) +
geom_line(size = 1) +
geom_point(aes(shape = class), size = 4)
There are 26 different point shapes you can use, numbered from 0 to 25. Some of these shapes (indicated in red below) allow you to customize their fill color using the fill
attribute.
If you want to select specific point shapes from the available options, you can use the scale_shape_manual
function to specify the numbers corresponding to the shapes you want to use for each line graph. For example, if you want to use shapes 8 to 10, you can specify them as follows.
ggplot(tb2, aes(date, value, colour = class)) +
geom_line(size = 1) +
geom_point(aes(shape = class), size = 4) +
scale_shape_manual(values = c(8,9,10))