1

For my Master thesis i work with trees. I got 36 trees, one tree is my main tree, ever further tree i got the distance and the azimut° in relation to the main tree.

When in now plot all the trees, to show where they grow in the relation to the main tree, the full circle of the plot seems to only have ~330°.

You can see it easily by looking at the labels (which are supposed to be the directions of the sky).

It seems to me that it is because of the tree nr.24, because it has the highest azimut° with 329.4°.

there is no error or any other problem.

How do i fix the plot to be a full circle with 360° ?

Code

library(ggplot2)

pie <- ggplot(Baumdaten, aes(x = distanz, label = Nr)) 

pie + coord_polar(theta = "y", start = 0) + 
  geom_point(mapping = aes(distanz,azimut), 
             size = 15*Baumdaten$baumumfang/max(Baumdaten$baumumfang), 
             col = "deepskyblue") +
  geom_text(y = Baumdaten$azimut, size = 5) +
  theme_bw(base_size = 15) +
  theme(axis.text.x = element_text(angle=0, vjust = 1, hjust=1, size = 15) )+
  # axis.line.y.left = element_line(),
  # axis.ticks.y.left = element_line()
  # ) +
  labs(x = "Distanz zum Zentralbaum [m]", y = NULL) +
  scale_y_continuous(breaks=c(0,45,90,135,180,225,270,315),
                     labels=c("N","NO","O","SO","S","SW","W","NW") ) + 
  ggtitle("") + 
  theme(plot.title = element_text(hjust = 0.5))

Data

Baumdaten <- structure(list(Nr = 1:36, baumumfang = c(166.42, 124.03, 117.75, 
130.31, 125.6, 153.86, 164.85, 147.58, 122.46, 109.9, 141.3, 
130.31, 125.6, 122.46, 114.61, 122.46, 139.73, 152.29, 103.62, 
119.32, 117.75, 133.45, 111.47, 117.75, 131.88, 166.42, 141.3, 
106.76, 103.62, 113.04, 150.72, 166.42, 153.86, 139.73, 125.6, 
122.46), baumhoehe = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), distanz = c(0, 3, 5.2, 
5, 10, 12.2, 13, 10.8, 13.8, 14.8, 17.7, 27.1, 27, 35, 14.6, 
24, 12.1, 18.4, 17.3, 26, 25, 29.5, 31.4, 34.1, 37, 46, 46, 43, 
48, 42, 41, 34.4, 25, 24, 20.5, 14.1), azimut = c(0, 113, 261.3, 
30.1, 255.3, 222.6, 180.9, 148, 133.2, 112.6, 117.1, 112, 103.3, 
101.4, 144.7, 119.4, 288.3, 286.5, 317.2, 303.3, 327.1, 314.7, 
325.3, 329.4, 323.5, 325.8, 318.4, 307.6, 301.7, 292, 288.5, 
286.5, 284.4, 277.3, 262.9, 242.8), Kronenverlichtung = c(40L, 
30L, 40L, 98L, 30L, 50L, 95L, 40L, 30L, 40L, 70L, 40L, 40L, 100L, 
40L, 60L, 40L, 95L, 100L, 20L, 20L, 5L, 40L, 100L, 15L, 100L, 
90L, 40L, 60L, 100L, 40L, 70L, 50L, 50L, 30L, 80L), Vergilbung = c(0L, 
0L, 0L, 0L, 0L, 0L, 3L, 1L, 0L, 0L, 1L, 2L, 0L, 0L, 1L, 0L, 1L, 
4L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 1L, 0L, 
0L, 0L, 3L), durchmesser = c(53, 39.5, 37.5, 41.5, 40, 49, 52.5, 
47, 39, 35, 45, 41.5, 40, 39, 36.5, 39, 44.5, 48.5, 33, 38, 37.5, 
42.5, 35.5, 37.5, 42, 53, 45, 34, 33, 36, 48, 53, 49, 44.5, 40, 
39)), row.names = c(NA, -36L), class = "data.frame")

The 330° circle:

the 330° circle

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
bentheman
  • 23
  • 4
  • Hi Ben. Welcome on SO. Could you provide your data and your code in a reproducible form instead of adding images. Simply paste your code in the post. Regarding the data. Simply type `dput(Baumdaten)` in the R console and copy the output (starting with `structure(...)`) into your post. See also https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to make a good reproducible example. – stefan Jun 17 '20 at 10:05

1 Answers1

1

Short answer: add limits = c(0, 360) to scale_y_continuous(). Your plot will look like what you appear to have in mind.

enter image description here

Longer answer: I re-formatted & streamlined your code to the following:

ggplot(Baumdaten, 
       aes(x = distanz, y = azimut, label = Nr)) +

  geom_point(aes(size = 15 * baumumfang/max(baumumfang)),
             col = "deepskyblue") +
  geom_text(size = 5) +

  coord_polar(theta = "y", start = 0) + 

  labs(title = "",
       x = "Distanz zum Zentralbaum [m]", 
       y = NULL) +
  scale_y_continuous(breaks = seq(0, 359, by = 45), # less typing for same result
                     labels = c("N", "NO", "O", "SO", "S", "SW", "W", "NW"),
                     limits = c(0, 360)) +

  theme_bw(base_size = 15) +
  theme(axis.text.x = element_text(hjust = 1, size = 15),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")  # hides all legends (necessary since size is
                                   # a mapped aesthetic now)

Some advice on coding practices when it comes to ggplot:

  1. Specify your aesthetic mappings for clarity. i.e. aes(x = distanz, y = azimut) instead of aes(distanz, azimut).

  2. Put common aesthetic mappings in the top level ggplot() so that all geom layers inherit them by default, to minimize repetition. i.e. aes(x = distanz, y = azimut) should be at the top level because both geom_point and geom_text layers use them.

  3. You can also put uncommon aesthetic mappings in the top level ggplot() if there's no confusion. i.e. aes(label = Nr) can be at the top level here, since you only have one text layer, but if you have multiple geom_text / geom_label layers that use different columns from your dataset, it would be simpler to specify these mappings in the respective geom layers instead.

  4. All mappings should go inside aes(), and without using the $ symbol. i.e. aes(size = 15 * baumumfang/max(baumumfang)) instead of size = 15 * Baumdaten$baumumfang / max(Baumdaten$baumumfang).

  5. Follow a consistent order in your ggplot layers, so that they are easier to keep track over time. This becomes particularly useful if you copy off an existing piece of code & modify different details all over the place. E.g. the ggplot2 cheatsheet on RStudio's website uses the following:

template

  1. Don't specify default details in theme elements. You can check what the defaults are with calc_element. e.g. running calc_element("axis.text.x", theme_bw(base_size = 15)) in console shows its angle / vjust are already 0 & 1 respectively, so you only have to specify theme(axis.text.x = element_text(hjust = 1, size = 15)).
Z.Lin
  • 28,055
  • 6
  • 54
  • 94