简体   繁体   中英

specifying color to individual points in a radial plot using plotrix package radial.plot

I have 2 lists of data.

a = c(0, 14400, 15000, 1600)
b = c(0, 1.1, 2.3, 4.5)

I would like to plot a graph using polar (radial) coordinates using radial plot. I want r=theta, so the values for the coordinates for r and theta are in the list "a".

For the color of the points I want to use list "b" to specify the colors of each point using conditions. So for example: If the value of the elemnt in b is less than 1, the color of that element is black. If the value of the element in b is between 1 and 2, the color is green. If the value of the element in b is greater than 3, the color is red.

So the resulting list would be:

c = c("black", "green", "red", "red")

The last step would be to use the list "c" to color the elements. In summary the answer to my question require 2 steps: 1) creating a new list "c" with a list of color names based on values of b 2) using list c in the radial plot to color each symbol in the plot (I don't know if this is possible)

Here is what I have so far.

pp <- radial.plot(a, a, start = -pi/3, rp.type = "s", clockwise = TRUE, point.symbols=19)
pp

The actual data is much larger, but I am providing only a small example code to focus on the 2 problems that I need to solve. I was able to generate this plot using ggplot2. However, I would prefer to use the radial.plot function in the plotrix package to this because of other reasons (the code for polar/radial coordinates seems to be much simpler and easier to use).

Please help, I'm new to R. Thank you.

This code should be flexible enough to generate the type of plot you want. It generates the figure shown below. For the angles I scaled them so that the maximum value of a would map to 2*pi. This will give a spiral look to the plot. Not doing that means that the angle will be the value in a modulo 2*pi, and with large numbers this appears random.

The second part about assigning colors can be done through assigning colors based on logical indexing. More conditions can be added as needed. Without a specific pattern it isn't simple to give a more general result for coloring.

在此处输入图片说明

library(plotrix)

# given example data
a = c(0, 14400, 15000, 1600)
b = c(0, 1.1, 2.3, 4.5)


# initializing vector of colors as NA
colors_plot <- rep(NA,length(b))

# set of conditions listed in the plot
colors_plot[b < 1] <- "black"
colors_plot[intersect(1 < b,b < 2)] <- "green" # need intersect because of double condition
colors_plot[b > 2] <- "red"
# add more conditions as needed

pp <- radial.plot(a, # radial distance
                  a*2*pi/max(a), # scaling so max(a) maps to 2*pi, this avoids the cooridnates wrapping
                  start = -pi/3, # same as code
                  rp.type = "s",
                  clockwise = TRUE,
                  point.symbols=19,
                  point.col=colors_plot, # colors calcualted above
                  radial.labels=NA # not plotting radial axis labels
                  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM