我希望控制x轴上出现的刻度线.以下代码以5的顺序(5,10,15 ... 30)放置刻度线
library(plotly)
df <- data.frame(x = 1:30,
y = sample(100:300, size = 30, replace = T))
p <- plot_ly(data = df, x = x, y = y, type = 'line') %>%
layout(title = 'Example plot')
p
Run Code Online (Sandbox Code Playgroud)
我需要按照6,12,18,24,30的顺序放置它们.我一直在浏览文档,但我似乎无法找到我需要的东西.在ggplot2这可以通过scale_x_continuous(breaks=c(6,12,18,24,30).
包rgl包含一个非常有用的函数ellipse3d,它可以返回一个覆盖 3D 中 95% 的点的椭球体。然后可以使用该对象来rgl::plot3d绘制它。我的问题是,是否可以将 的输出转换ellipse3d为可以通过 js 绘图包(如plotly)绘制的内容?
library(rgl)
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
plot3d(dt)
plot3d(ellipse, add = T, color = "red", alpha = 0.5)
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做才能通过绘图绘制椭球体呢?
我使用ropensci/plotly 库创建了一个表格。我尝试通过多种方式添加标题。
这是我的代码:
library(plotly)
data <- read.csv("data.csv")
plot_ly(
type = 'table',
columnwidth = c(33, 32, 32),
columnorder = c(0, 1, 2),
header = list(
values = list(list(c("1st column")),
list(c("2nd column")),
list(c("3rd column"))
),
align = c("center", "center"),
line = list(width = 2, color = 'black'),
fill = list(color = c("blue", "blue")),
font = list(family = "Arial", size = 14, color = "white")
),
cells = list(
values = list(c(data$1st_column),c(data$2nd_column),c(data$3rd_column)),
align = c("center", "center"),
line = list(color = "black", width = …Run Code Online (Sandbox Code Playgroud) 我一直在尝试制作一张美国地图,其中包含枪击事件的叠加散点图,每个点的 hoverinfo 标签中都有伤亡人数和受伤人数。但是,标签弹出为“num_killed”,我想将其格式化为“Number Killed:”。到目前为止,我已经尝试在 ggplot 部分标记变量,但无济于事。有什么方法可以更改工具提示中变量名称的打印方式吗?
这是我的代码:
library(plotly)
library(dplyr)
library(ggplot2)
us <- map_data("state")
library(maps)
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured)) +
labs(color = "Number Injured", size = "Number Killed", num_killed = "Number Killed", num_injured = "Number Injured")
plot <- ggplotly(g)
Run Code Online (Sandbox Code Playgroud)
所附数据集是该数据库处理后的 csv:http : //www.gunviolencearchive.org/reports/mass-shooting? page =1 (作为数据框处理)。
我需要在散点图上绘制加速度与英里/加仑的两个斜率,一个用于轻型汽车,一个用于重型汽车。我创建了这个:
cars_light <- cars_log[cars_log$log.weight. <= log(mean(cars$weight)), ]
cars_heavy <- cars_log[cars_log$log.weight. > log(mean(cars$weight)),]
cars_log$wt_cat <- ifelse(cars_log$log.weight. > log(mean(cars$weight)), 'heavy', 'light')
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经通过这样做创建了散点图:
plot_ly(
data = cars_log,
type = "scatter",
x = ~log.acceleration.,
y = ~ log.mpg.,
color = ~ factor(wt_cat),
colors = c("#8bc34a", "#ff5722"),
marker = list(size = 10, opacity = 0.6)
) %>%
layout(title = "Heavy cars VS light cars")
Run Code Online (Sandbox Code Playgroud)
这给了我这个结果:
现在,我想为重型汽车创建一个斜坡,为轻型汽车创建另一个斜坡,我知道我需要使用 add_ribbons 轨迹跟踪,但我不知道如何生成它。我在用情节计算 lm 时遇到问题。我可以用 ggplot 做同样的事情,但我不知道如何用plotly 做到这一点。
ggplot(cars_log, aes_string('log.acceleration.', 'log.mpg.')) +
geom_point(aes(color = factor(wt_cat))) +
geom_smooth(method = 'lm', aes(color …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用data.frameR 中的两个不同绘制折线图plotly。问题是两者data.frame都有不同的维度。data.frame1 的示例数据:
DATE SOC resdiff
2016 2017-08-11 02:40:00 95.45 0.54
4033 2017-08-18 02:45:00 94.88 0.56
6048 2017-08-25 02:45:00 94.28 0.60
8064 2017-09-01 02:45:00 93.68 0.60
10080 2017-09-08 02:45:00 92.96 0.72
12096 2017-09-15 02:45:00 92.13 0.83
Run Code Online (Sandbox Code Playgroud)
data.frame2 的样本数据:
data.event_type data.user data.stamp
1 config *INST* 2018-06-27 14:37:29
2 config *INST* 2018-02-14 19:30:57
3 config *SYNC* 2017-12-18 07:00:53
4 config *SYNC* 2017-12-18 06:59:14
5 config *INST* 2017-10-03 00:55:25
6 config *INST* 2017-09-28 00:49:29 …Run Code Online (Sandbox Code Playgroud) 我使用 ggplotly 在 R 中创建了一个地图。要创建链接,其大小需要为 524kb 或以下,但目前为 1.2Mb。有什么好的方法可以减少文件大小以便我可以导出吗?或者这完全不现实?
我正在绘图中创建一个 3D 散点图,并希望有一个几个句子长的标题(为了描述图形),标题左对齐,不干扰图形本身,并且不被地块面积。
下面的代码在plotly中包含了关于左对齐标题的stackoverflow答案,但是当你有一个需要超过3-4行的长标题时,它没有帮助。
使用 \n 会使标题的文本转到第二行,但它仍然会被绘图区域截断并且不会左对齐。
library(plotly)
metric1 <- c(116,230,120,200,210,188,130,99,101,120)
metric2 <- c(79,109,120,95,130,98,118,130,140,88)
metric3 <- c(30,28,42,22,6,2,17,43,20,28)
class <- c(3,4,2,1,1,4,4,3,2,3)
df <- data.frame(metric1,metric2,metric3,class)
my_colors=c("red", "blue", "green", "#000000")[df$class]
p <- plot_ly(df,
x = ~metric1,
y = ~metric2,
z = ~metric3, text = class, type = "scatter3d",
mode = "markers", marker = list(color = my_colors)) %>%
add_annotations(
x=0, y=1.15,
text="Figure: The title of the figure will explain what information can be gleaned from the figure. Then the next sentence, which is still …Run Code Online (Sandbox Code Playgroud) 我使用 ggplot2 使用以下代码绘制了以下两个图表。
p <- ggplot() +
geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsBefore, x = c(1:41), group = 1, color = "Clients 3 Months Before"),
size = 1 ) +
geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsAfter, x = c(1:41), group = 1, color = "Clients 3 Months After"),
size = 1 ) +
xlab('Campaigns') + ylab('Clients') + ggtitle('Clients 3 months before and after the campaigns') +
scale_x_continuous(breaks = seq(1,41,1)) + scale_y_continuous(limits=c(0, 200)) +
theme(legend.title=element_blank())
q <- ggplot() +
geom_line(data = …Run Code Online (Sandbox Code Playgroud) 我想使用 purrr 包中的地图将 y 轴轨迹添加到绘图对象。但它不是向现有绘图添加新迹线,而是为每个迹线创建单独的绘图。看:
library(purrr)
library(plotly)
data("iris")
p = plot_ly(iris, type = "bar")
xaxis = ~Species
map(c(~Sepal.Length, ~Sepal.Width, ~Petal.Length, ~Petal.Width), ~add_trace(p, x = xaxis, y = .x))
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?