Plotly中使用add_markers函数为散点添加边框的方法
add_markers() in Plotly R No worries, I’ve been there too! When building plots incrementally with add_markers(), you just need to nest the line parameters inside the marker argument of add_markers()—the same way you did in your original plot_ly() call. Here's how to tweak your code to get that desired border:
plot_ly(data = iris) %>% add_markers( x = ~Sepal.Length, y = ~Petal.Length, marker = list( color = 'rgba(255, 182, 193, .9)', # Your original fill color size = 10, # Optional: match the size from your ideal implementation line = list( color = 'rgba(152, 0, 0, .8)', # Border color width = 2 # Border width ) ) )
Why this works
The add_markers() function accepts a marker list parameter, just like the initial plot_ly() call. All marker-related properties (fill color, size, border line) go inside this list, keeping it consistent with how you define markers in simpler plots. This approach is perfect for complex scenarios where you’re adding multiple layers or customizing plots step-by-step.
If you ever need to map colors to a data variable instead of using a fixed color, you can still keep the line settings inside the marker list—just move the color argument outside the list (e.g., color = ~Species) and define the border properties within marker$line.
内容的提问来源于stack exchange,提问作者potockan

