Enhancing Graph Points in R Studio: Adding Distinct Borders

Enhancing Graph Points in R Studio: Adding Distinct Borders


I have datasets below. This data pertains to the differences in grain yield and height resulting from various fertilizer treatments.

library(readr)
github= "https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/Fertilizer%20(One%20Way%20ANOVA).csv"
dataA= data.frame(read_csv(url(github),show_col_types = FALSE))

dataA
   Fertilizer Yield Height
1     Control  12.2     45
2     Control  12.4     52
3     Control  11.9     42
4     Control  11.3     35
5     Control  11.8     40
6     Control  12.1     48
7     Control  13.1     60
8     Control  12.7     61
9     Control  12.4     50
10    Control  11.4     33
11       Slow  16.6     63
12       Slow  15.8     50
13       Slow  16.5     63
14       Slow  15.0     33
15       Slow  15.4     38
16       Slow  15.6     45
17       Slow  15.8     50
18       Slow  15.8     48
19       Slow  16.0     50
20       Slow  15.8     49
21       Fast   9.5     52
22       Fast   9.5     54
23       Fast   9.6     58
24       Fast   8.8     45
25       Fast   9.5     57
26       Fast   9.8     62
27       Fast   9.1     52
28       Fast  10.3     67
29       Fast   9.5     55
30       Fast   8.5     40

Now I’ll create a graph using the following code.

library(ggplot2)
ggplot(data=dataA, aes(x=Height, y=Yield)) +
  geom_point (aes(color=Fertilizer)) +
  scale_x_continuous(breaks= seq(0,80,10),limits= c(0,80)) +
  scale_y_continuous(breaks= seq(0,20,5), limits= c(0,20))

Since I assigned the color based on the condition of the “Fertilizer,”; geom_point (aes(color=Fertilizer)), the colors are automatically distinguished, resulting in the graph being plotted. However, I would like to add borders to these points in order to represent them more distinctly.

By the way, if you include shape=Fertilizer within aes(), R will automatically differentiate the shapes of Fertilizer as well.

ggplot(data=dataA, aes(x=Height, y=Yield)) +
  geom_point (aes(color=Fertilizer, shape=Fertilizer)) +
  scale_x_continuous(breaks= seq(0,80,10),limits= c(0,80)) +
  scale_y_continuous(breaks= seq(0,20,5), limits= c(0,20))


From this point onwards, I’ll be adding borders to the points on the graph. Hence, I will remove color=Fertilizer from within aes() and place the color specification outside of aes(), as shown in the following code.

ggplot(data=dataA, aes(x=Height, y=Yield)) +
  geom_point (aes(shape=Fertilizer), color="black") +
  scale_x_continuous(breaks= seq(0,80,10),limits= c(0,80)) +
  scale_y_continuous(breaks= seq(0,20,5), limits= c(0,20))

In this scenario, the automatically assigned colors for each Fertilizer category will be replaced, and all will adopt the black color that you’ve specified for the borders. To maintain this black color exclusively for the borders while modifying the filling color, I will utilize the scale_fill_manual() to alter the point colors.

ggplot(data=dataA, aes(x=Height, y=Yield)) +
  geom_point (aes(fill= Fertilizer), color="black") +
  scale_fill_manual(values = c("Blue","Gray","Red"))+
  scale_x_continuous(breaks= seq(0,80,10),limits = c(0,80)) +
  scale_y_continuous(breaks= seq(0,20,5), limits = c(0,20))

I will now proceed to adjust the shape of the points using scale_shape_manual(). For this purpose, I will make use of shapes 21, 22, and 24. These shapes allow for modifications such as circles, squares, and triangles, respectively.

ggplot(data=dataA, aes(x=Height, y=Yield)) +
  geom_point (aes(shape= Fertilizer, fill= Fertilizer), color="black", size=4) +
  scale_fill_manual(values = c("Blue","Gray","Red"))+
  scale_shape_manual(values = c(21,24,22)) +
  scale_x_continuous(breaks= seq(0,80,10),limits = c(0,80)) +
  scale_y_continuous(breaks= seq(0,20,5), limits = c(0,20))

The borders will be configured as black. When compared to the version without borders, the graph with borders appears to be clearer.


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.