gpt4 book ai didi

R绘图极坐标图简单定制

转载 作者:行者123 更新时间:2023-12-02 13:19:58 24 4
gpt4 key购买 nike

嗨,R 和阴谋专家,

我正在尝试plotly最近。

这是我简单的最小设计代码:

library(plotly)

df <- data.frame(
r = c(0,1,2,2,1.5,3, 1, 2, 3, 4, 5, 6),
theta = c(0,45,90,315, 180, 270, 10, 40, 70, 100, 130, 160),
group = c('R', 'B', 'G', 'R', 'G', 'B', 'R', 'B', 'G', 'R', 'G', 'B'),
size = c(1, 2, 3, 10, 20, 9, 2, 4, 6, 8, 10, 12)
)

colors_map <- c("blue", "green", "red")

p <- plot_ly(
df,
type = 'scatterpolar',
r = ~r,
theta = ~theta,
color = ~group,
colors = colors_map, #Provide a color mapped vector
size = ~size,
sizes = c(100,200), #Provide a range of size.
text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size),
mode = 'markers',
hoverinfo="text",
marker = list(symbol = 'circle', line = list(width = 2, color = 'lightgrey'))
)%>%
layout(title = 'SAMPLE TITLE',
showlegend = TRUE,
polar = list(
#hole = 0.5,
radialaxis = list(
tickfont = list(
size = 20,
color = "blue"
),
visible = TRUE,
tickcolor = toRGB("red"),
ticks = "outside",
#ticklen = 15,
#tickwidth = 20,

range = c(0, max(df$r)),
rangemode = "tozero",
angle = 90,
tickangle = 90,
#gridcolor = '#FFF', #color of grid lines
categoryorder = "array",
categoryarray = c("GroupA", "GroupB", "GroupC", "GroupD") #Not working
),
angularaxis = list(
tickfont = list(
size = 12
),
rotation = 90,
direction = 'clockwise' ,
categoryarray = c("A", "B", "C", "D") #Not working
)
)
)
ggplotly(p)

绘制的图表是:

enter image description here

我尝试过一些基于文档的方法,但无法实现。以下是我未能渲染的项目[其中很少有已解决并发布在答案中]:

  1. [已解决]根据点所属的组显示点的颜色。如果属于R组,则颜色应为红色,如果 B然后蓝色,如果 G然后是绿色。

  2. [已解决]极坐标图中显示的点的大小非常小,有办法放大它或给出大小范围吗?

  3. [已解决] 在每个点的中间,我想显示文本/数字,假设我想显示 size 每个点中心的值。我怎样才能做到这一点?

  4. 删除基于角度的角轴标签,并希望将我的自定义标签设置为 A , B , C , D分别为 45 度、135 度、225 度和 315 度。

  5. [已解决] 在极坐标图外部的垂直线上显示径向轴标签,而不是它在图表内的显示方式。它应该以垂直规则形式位于图表之外,使用我的自定义标签,比如说 GroupA , GroupB , GroupC , GroupD .

  6. [已解决] 使鼠标悬停工具提示放大并在其中显示更多信息。例如,现在,group信息显示在工具提示之外,它应该在行内。正在删除theta来自工具提示。

  7. 如何制作一个填充灰色并在其上写上文字的同心圆?与ggplot我使用 geom_rect 实现它在图表底部,然后 coord_polar ,但这种方法在 polar chart 中不起作用与 plotly 。还有其他方法吗?

我知道这可能是 plotly 的基本内容专家们,但自过去 4 小时以来,我一直在苦苦挣扎,并使事情复杂化。

我想要类似如下的内容: enter image description here我确信,这个问题将在帮助初学者自定义极坐标图表视觉效果方面大有帮助,因为没有可用的在线帮助,所以问题解决了它。如有任何帮助,我们将不胜感激。

最佳答案

到目前为止,我能够解决已解决制作的项目。

R 绘图代码:

library(plotly)

df <- data.frame(
r = c(0,1,2,2,1.5,3, 1, 2, 3, 4, 5, 6),
theta = c(0,45,90,315, 180, 270, 10, 40, 70, 100, 130, 160),
group = c('R', 'B', 'G', 'R', 'G', 'B', 'R', 'B', 'G', 'R', 'G', 'B'),
size = c(1, 2, 3, 10, 20, 9, 2, 4, 6, 8, 10, 12)
)

colors_map <- c("blue", "green", "red")

p <- plot_ly(
df,
type = 'scatterpolar',
r = ~r,
theta = ~theta,
color = ~group,
colors = colors_map, #Provide a color mapped vector
size = ~size,
sizes = c(100,200), #Provide a range of size.
text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size),
mode = 'markers',
hoverinfo="text",
marker = list(symbol = 'circle', line = list(width = 2, color = 'lightgrey'))
)%>%
layout(title = 'SAMPLE TITLE',
showlegend = TRUE,
polar = list(
#hole = 0.5,
radialaxis = list(
tickfont = list(
size = 20,
color = "blue"
),
visible = TRUE,
tickcolor = toRGB("red"),
ticks = "outside",
#ticklen = 15,
#tickwidth = 20,

range = c(0, max(df$r)),
rangemode = "tozero",
angle = 90,
tickangle = 90,
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
#gridcolor = '#FFF', #color of grid lines
#categoryorder = "array",
#categoryarray = c("GroupA", "GroupB", "GroupC", "GroupD") #Not working
),
angularaxis = list(
tickfont = list(
size = 12
),
rotation = 90,
direction = 'clockwise' ,
categoryarray = c("A", "B", "C", "D") #Not working
)
)
)%>%
add_trace(
r = ~r,
theta = ~theta,
mode = "text",
text = ~size,
textfont = list(color = '#000000', size = 12)
) %>%
add_trace(
r = ~r,
theta = ~theta,
mode = "marker",
opacity = 0.5,
text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size),
hoverinfo = "text"

)

ggplotly(p)

图表: enter image description here

关于R绘图极坐标图简单定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55652449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com