R 散点图标记绘制距离刻度和轴

问题描述 投票:0回答:1

为什么刻度线和轴之间的距离这么大?此外,为什么这个图表从 0 开始?

library(plotly)

# Sample data
df <- data.frame(
  x = c(0, 1, 2, 3, 4, 5),
  y = c(0, 10, 14, 18, 24, 30)
)

# Create a scatter plot with markers
fig <- plot_ly(df, x = ~x, y = ~y, type = 'scatter', mode = 'markers',
               marker = list(size = 10, color = 'blue'), 
               text = ~paste('Point ', seq_along(x)))

# Show the plot
fig

r plotly scatter-plot
1个回答
0
投票

我们可以使用

layout()
函数更改此设置:

plot_ly(df, x = ~x, y = ~y, type = 'scatter', mode = 'markers',
               marker = list(size = 10, color = 'blue'), 
               text = ~paste('Point ', seq_along(x))) %>%
  layout(
  xaxis = list(range = c(0, max(df$x))),
  yaxis = list(range = c(0, max(df$y))),
  margin = list(l = 10, r = 10, b = 10, t = 10, pad = 4)
)

© www.soinside.com 2019 - 2024. All rights reserved.