我正在使用 R 编程语言。我使用“plotly”库制作了以下 3 维图:
library(dplyr)
library(plotly)
my_function <- function(x,y) {
final_value = (1 - x)^2 + 100*((y - x^2)^2)
}
input_1 <- seq(-1.5, 1.5,0.1)
input_2 <- seq(-1.5, 1.5,0.1)
z <- outer(input_1, input_2, my_function)
plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()
Run Code Online (Sandbox Code Playgroud)
我现在尝试将“轮廓线”添加到上面的图中,如下所示:https://plotly.com/r/3d-surface-plots/
我正在尝试调整“plotly 网站”中的代码来制作这些轮廓,但我不确定如何做到这一点:
图一:
# This might have worked?
fig <- plot_ly(z = ~z) %>% add_surface(
contours = list(
z = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(z=TRUE)
)
)
)
fig <- fig %>% layout( …Run Code Online (Sandbox Code Playgroud) 我在 R 中有这些数据:
string_1 = c("newyork 123", "california 123", "washington 123")
string_2 = c("123 red", "123 blue", "123 green")
my_data = data.frame(string_1, string_2)
Run Code Online (Sandbox Code Playgroud)
我想string_2从中“减去” string_1。结果看起来像这样:
"newyork", "california", "washington"
Run Code Online (Sandbox Code Playgroud)
我尝试这样做:
library(tidyverse)
# did not work as planned
> str_remove(string_1, "string_2")
[1] "newyork 123" "california 123" "washington 123"
Run Code Online (Sandbox Code Playgroud)
但这并不是执行“完整”减法。
谢谢你!
我创建了一个名为“widgets_t.html”的 html 文件(几个情节图组合在一起)。使用本教程(https://beta.rstudioconnect.com/jjallaire/htmlwidgets-showcase-storyboard/htmlwidgets-showcase-storyboard.html)作为演示以及此处提供的答案(如何在 flexdashboard 中创建下拉菜单?),我尝试创建一个 Rmarkdown/Flexdashboard 文档。这是我正在使用的代码(在本例中,为了简洁起见,我只是使用了相同的 html 输入和相同的文本):
---
title: "maps"
output:
flexdashboard::flex_dashboard:
storyboard: true
social: menu
source: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{css}
.storyboard-nav .sbframelist ul li {
height: auto;
}
```
Page 1
=====================================
Column {.tabset}
-------------------------------------
### Title 1
<object type="text/html" width="1500" height="1500" data="widgets_t.html"></object>
https://rstudio.github.io/leaflet/
- Interactive panning/zooming
- Compose maps using arbitrary combinations of map tiles, markers, polygons, lines, popups, and GeoJSON.
- Create maps right from the R …Run Code Online (Sandbox Code Playgroud) 我在 R 中编写了一个循环来生成 10 个图:
library(plotly)
for (i in 1:10)
{
d_i = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))
title_i = paste0("title_",i)
p_i = plot_ly(data = d_i, x = ~x, y = ~y) %>% layout(title = title_i)
htmlwidgets::saveWidget(as_widget(p_i), paste0("plot_",i, ".html"))
}
Run Code Online (Sandbox Code Playgroud)
我有这段代码(输入菜单内容不会溢出 flexdashboard 中的行框),它在 R 中创建了仪表板:
---
title: "Test Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Column {data-width=100}
-----------------------------------------------------------------------
### Window 1
```{r}
selectInput("project", label = NULL, choices = …Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。
我有以下数据:
library(dygraphs)
var1 = c(5,5,8,13,14,12,11,10,9,60, 50,40)
var2 = c(13,13,22,30,35,31,25,24,20,20,20,21)
var1 <- ts(var1, start = 2010, frequency = 12)
var2 <- ts(var2, start = 2010, frequency = 12)
df2 = cbind(var1, var2 = var2 - var1)
Run Code Online (Sandbox Code Playgroud)
然后我制作了以下堆积图(基于此处提供的答案(/sf/answers/5392919171/):
dygraph(df2, main = "Graph") %>%
dySeries("var1", label = "Var1") %>%
dySeries("var2", label = "Var2") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
Run Code Online (Sandbox Code Playgroud)
我的问题:我可以看到 y 轴上绿线的值与标签(右上角)中的值不匹配。但是,y 轴本身的值是正确的。
我可以做什么来解决这个问题?
谢谢!
我模拟了以 50 为中心的正态分布中的数字,然后仅将数字保留在 0 到 100 之间:
mean <- 50
sd <- 15
n <- 100000
set.seed(123)
numbers <- rnorm(n, mean, sd)
numbers <- pmax(0, pmin(numbers, 100))
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,结果看起来像钟形曲线:
然后,我想看看每个单位间隔之间的数字百分比是多少:
min_interval <- seq(0, 99, by = 1)
max_interval <- seq(1, 100, by = 1)
count <- sapply(min_interval, function(x) {
sum(numbers >= x & numbers < (x + 1))
})
percentage <- count / length(numbers) * 100
df <- data.frame(min_interval = min_interval,
max_interval = max_interval,
count = count,
percentage = percentage)
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样: …
我正在使用 R 编程语言工作。
我正在尝试为以下函数绘制从 n=0 到 n = 1000 的图(链接:https://www.youtube.com/watch?v =iH2kATv49rc&t=7s ):
首先,我尝试定义 P_00(2n) 部分:
combinatorial_square <- function(n) {
(choose(2*n, n)^2) * ((1/4)^(2*n))
}
Run Code Online (Sandbox Code Playgroud)
然后,我尝试编写另一个函数来执行上述函数的累积和:
cumulative_sum <- function(N) {
s <- 0
for (n in 1:N) {
s <- s + (choose(2*n, n)^2) * ((1/4)^(2*n))
}
return(s)
}
Run Code Online (Sandbox Code Playgroud)
然后,我尝试将其绘制在 N 的范围内:
N <- 1:250
y <- sapply(N, cumulative_sum)
plot(N, y, type = "l", main = "Plot of cumulative_sum function", xlab = "N", ylab = "cumulative_sum(N)")
Run Code Online (Sandbox Code Playgroud)
我的问题:我似乎无法针对较大的 N 值绘制此函数(我认为这可能是因为计算机无法计算大的组合项?): …
我有一个在 R 中看起来像这样的数据集:
address = c("882 4N Road River NY, NY 12345", "882 - River Road NY, ZIP 12345", "123 Fake Road Boston Drive Boston", "123 Fake - Rd Boston 56789")
name = c("ABC Center Building", "Cent. Bldg ABC", "BD Home 25 New", "Boarding Direct 25")
my_data = data.frame(address, name)
address name
1 882 4N Road River NY, NY 12345 ABC Center Building
2 882 - River Road NY, ZIP 12345 Cent. Bldg ABC
3 123 Fake Road Boston Drive …Run Code Online (Sandbox Code Playgroud) 我有兴趣学习如何在 R 中使用道路网络文件。
例如,我有兴趣找出以下两个(加拿大)地址之间的驾驶距离:
过去,我会使用 OpenStreetMap (OSM) 等 API:
library(tmap)
library(tmaptools)
remotes::install_github("riatelab/osrm")
q1 = geocode_OSM("6301 Silver Dart Dr, Mississauga, ON L5P 1B2")
q2 = geocode_OSM("290 Bremner Blvd, Toronto, ON M5V 3L9")
q1 = as.numeric(q1$coords)
q2 = as.numeric(q2$coords)
q1_lat = q1[1]
q1_long = q1[2]
q2_lat = q2[1]
q2_long = q2[2]
route = osrmRoute(src = c(q1[1], q1[2]) , dst = c(q2[1], q2[2]), osrm.profile = "car")
> route$distance …Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。
我正在尝试制作网络/树可视化,显示位于我的计算机上的所有文件夹、子文件夹和文件。
我发现以下代码可以列出我的计算机上的所有文件/文件夹:
dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)
Run Code Online (Sandbox Code Playgroud)
从这里,我想制作一个网络图可视化(例如使用 igraph)来显示文件夹、子文件夹和文件的所有路径:
https://www.reddit.com/r/dataisbeautiful/comments/ajcb4n/i_made_a_graph_visualization_of_my_projects/
我在这里找到这篇文章,展示了如何在有限的设置下执行此操作:How to build a dendrogram from a Directory Tree?
但我试图直接为此声明执行此操作
dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我该怎么做吗?
谢谢!
我的尝试:
library(igraph)
library(data.tree)
dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)
edges <- data.frame(from = dirname(dirs), to = basename(dirs), stringsAsFactors = FALSE)
g <- graph_from_data_frame(edges, directed=TRUE)
plot(g, layout=layout_as_tree)
Run Code Online (Sandbox Code Playgroud) r ×10
plotly ×2
r-markdown ×2
3d ×1
duplicates ×1
fuzzy-logic ×1
geospatial ×1
html ×1
math ×1
shapefile ×1
shiny ×1
sql ×1
string ×1
text ×1