0

I have never plotted a radial in R before, so I have no idea what I am doing. Is there a different type of plot I should be using? Also I have a list of r's and theta's, however I am not sure how to create a loop in order to transform them all to Cartesian coordinates?

Data

"theta","r"
0,4
0.31,2.07
0.63,2.15
0.94,3.08
1.26,4.09
1.57,2.39
1.88,1.96
2.2,3.72
2.51,4.72
2.83,2.87
3.14,1.67
3.46,3.51
3.77,4.37
4.08,2.24
4.4,2.23
4.71,4.18
5.03,3.12
5.34,2.67
5.65,2.19
5.97,3.16
Fire
  • 301
  • 1
  • 2
  • 9
  • It depends on the data and what you're trying to get out of it. StackOverflow might not be the best place for it, since it's more about data science and visualization than about problems with programming. If you have sample data and some idea of what you are trying to see in the data, then it would be useful. – r2evans Dec 15 '19 at 03:19
  • @r2evans I added the data. – Fire Dec 15 '19 at 03:21
  • Multiplication and trig functions are vectorized, you don't need a loop. `data$x = data$r * cos(data$theta); data$y = data$r * sin(data$theta); with(data, plot(x, y, type = "l")` – Gregor Thomas Dec 15 '19 at 03:39
  • @Gregor sorry, your transformation DID work, but the plot did not. – Fire Dec 15 '19 at 04:03
  • Gregor's plot worked for me once I added a missing close-paren at the end. – r2evans Dec 15 '19 at 04:10
  • Something else wrong with your function: in your `for` loop (see Gregor's comment about vectorizing it), you assign to `z` and `y` each time a single value. If you must do it in a loop, then create a vector of the same length as the calculations (e.g., `z <- numeric(nrow(x))`, then assign to its index (`z[i] <- ...`). – r2evans Dec 15 '19 at 04:12
  • @r2evans I updated my code, and with my current code I get the following error: Error in plot(x, y, type = "l") : unused arguments (y, type = "l") – Fire Dec 15 '19 at 04:14
  • Your code above works for me without errors. – r2evans Dec 15 '19 at 04:19
  • @r2evans Why am I getting an error then? Is it that I'm using R studio? Is it that I don't have a certain library added? – Fire Dec 15 '19 at 04:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204280/discussion-between-r2evans-and-cynical-f). – r2evans Dec 15 '19 at 04:28

1 Answers1

0

The ggplot2 package allows for plotting in polar coordinates quite conveniently:

library(ggplot2)
ggplot(DF, aes(theta, r)) +
  geom_line() +
  coord_polar()
  

enter image description here

coord_polar() does all the transformation.

Data

DF <- data.table::fread("theta,r
                  0,4
                  0.31,2.07
                  0.63,2.15
                  0.94,3.08
                  1.26,4.09
                  1.57,2.39
                  1.88,1.96
                  2.2,3.72
                  2.51,4.72
                  2.83,2.87
                  3.14,1.67
                  3.46,3.51
                  3.77,4.37
                  4.08,2.24
                  4.4,2.23
                  4.71,4.18
                  5.03,3.12
                  5.34,2.67
                  5.65,2.19
                  5.97,3.16")
Community
  • 1
  • 1
Uwe
  • 41,420
  • 11
  • 90
  • 134