小编Nat*_*ate的帖子

动画的控制速度

使用时,我想减慢状态之间的转换速度library(gganimate)

这是一个迷你示例:

# devtools::install_github("thomasp85/gganimate")
library(gganimate) # v0.9.9.9999

dat_sim <- function(t_state, d_state) {
  data.frame(
    x = runif(1000, 0, 1),
    y = runif(1000, 0, 1),
    t_state = t_state*d_state
    )
}

dat <- purrr::map_df(1:100, ~ dat_sim(., 1))

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = c(.3, .7),
       y = c(.3, .7)) +
  theme(legend.position = "none") +
  transition_time(t_state)
Run Code Online (Sandbox Code Playgroud)

太快

我的理想行为会慢很多(10-100倍),因此颜色会逐渐演变,没有人会发作。

如果我尝试使用transition_states()更多的手动控制,则会得到带有大部分空白帧的gif。我已经尝试了各种组合transition_legnth=state_length=但效果并不明显。

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

11
推荐指数
1
解决办法
2550
查看次数

tsibble——当没有隐式间隙时,如何解决隐式间隙

我是 tsibble 包的新手。我有每月数据,我将其强制转换为 tsibble 以使用寓言包。我遇到的一些问题

  • 看来索引变量(根据我的测试)不属于类日期,即使我对其应用了 lubridate 的 ymd 函数。
  • has_gaps 函数返回 FALSE,但是当我对数据建模时,我收到错误“.data 包含隐式时间间隙”
library(dplyr)
library(fable)
library(lubridate)
library(tsibble)

test <- data.frame(
   YearMonth = c(20160101, 20160201, 20160301, 20160401, 20160501, 20160601,
                 20160701, 20160801, 20160901, 20161001, 20161101, 20161201),
      Claims = c(13032647, 1668005, 24473616, 13640769, 17891432, 11596556,
                 23176360, 7885872, 11948461, 16194792, 4971310, 18032363),
     Revenue = c(12603367, 18733242, 5862766, 3861877, 15407158, 24534258,
                 15633646, 13720258, 24944078, 13375742, 4537475, 22988443)
)

test_ts <- test %>% 
  mutate(YearMonth = ymd(YearMonth)) %>% 
  as_tsibble(
    index = YearMonth,
    regular = FALSE       #because …
Run Code Online (Sandbox Code Playgroud)

r time-series fable tsibble

7
推荐指数
1
解决办法
4138
查看次数

R ggplot,geom_text 在标签命令中圆形标签?

现在我可以通过 ggplot geom_bar 中的条形获取上面填充值的标签。但我似乎无法做的是更改标签的小数位数。现在它有 9 位小数,而我只想要 2 位。我尝试使用 round() 和digits=,但没有成功。我认为我不确定将它们放在我的代码中的哪里。

比例是我想要仅标记小数点后两位的比例

谢谢你!

PaperMazeProportion <- ggplot(PM_df, aes(factor(OrientationNum), Proportion, fill=Choice)) +
  stat_summary(fun.y = mean, geom = "bar", position = "dodge") +
  labs(x= "Orientation of Maze",
       title = "Paper Maze Path Choice Penn State; May 07, 2018") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(limits= c(0, 0.65)) +
  scale_x_discrete(labels=c("North-South", "South-North", "East-West", "West-East"), breaks=c("1", "2", "3", "4")) +
  geom_text(aes(label=Proportion), position=position_dodge(width=0.9), digits=1, vjust=-0.25, size=3)
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-bar geom-text

6
推荐指数
0
解决办法
1万
查看次数

R Shinydashboard 删除图形周围的空白边框

我用来ggplot2创建图形并将它们嵌入到shinydashboard. 目标是让一切尽可能“透明”,其中涉及将背景设置为themerenderPlot仪表板背景相同的十六进制代码:

library(shiny)
library(shinydashboard)
library(ggplot2)

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(),
  dashboardBody(
    column(4, plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(mpg, aes(displ, hwy, colour = class)) + 
      geom_point() +
      theme(plot.background = element_rect(fill = "#ECF0F5"))
  }, bg="transparent")
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

这会产生以下输出,但是,图形的 3 个边周围有一个非常细的空白边框,如下所示(我放大并将空白更改为红色以提高可见度。在实际输出中,它是白色的)。

在此输入图像描述

如何消除这个空白区域?

r spacing shiny shinydashboard

5
推荐指数
0
解决办法
418
查看次数

直方图中条形组之间的间距

当我在 ggplot2 中生成直方图(其中条形位置为 )时dodge,我期望类似这样的情况,条形组之间有空间(即注意每组红/绿对之间的空白):

在此输入图像描述

当我用连续数据构建直方图时,我很难产生相同的效果。我似乎无法在酒吧组之间添加空间,相反,所有东西都被挤在一起。正如您所看到的,这使得在视觉上很难比较红色/绿色对:

在此输入图像描述

为了重现我的问题,我在此处创建了一个示例数据集:https ://www.dropbox.com/s/i9nxzo1cmbwwfsa/data.csv?dl=0

重现代码:

data <- read.csv("https://www.dropbox.com/s/i9nxzo1cmbwwfsa/data.csv?dl=1")

ggplot(data, aes(x = soldPrice, fill = month)) +
    geom_histogram(binwidth=1e5, position=position_dodge()) +
    labs(x="Sold Price", y="Sales", fill="") +
    scale_x_continuous(labels=scales::comma, breaks=seq(0, 2e6, by = 1e5)) +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
Run Code Online (Sandbox Code Playgroud)

如何在红/绿对组之间添加空白?

r ggplot2

5
推荐指数
1
解决办法
5584
查看次数

情节 - 表面 - 文字悬停信息不起作用

我已经用 plotly 构建了一个表面图,我正在尝试根据我自己的文本来设置 hoverinfo。奇怪的是它不再起作用了。

library(plotly)
x <- rnorm(10)
y <- rnorm(10)
z <- outer(y, x)

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = "surface",
             text = ~paste0("My X = ", x, "\n My Y = ", y, "\n My Z = ", z),
             hoverinfo = "text") %>% layout(dragmode = "turntable")
print(p)
Run Code Online (Sandbox Code Playgroud)

虽然

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = "surface") %>% layout(dragmode = "turntable")
Run Code Online (Sandbox Code Playgroud)

效果很好。

我也试过用\nby代替<br …

r plotly

2
推荐指数
1
解决办法
1091
查看次数

从read.csv更改为read_csv以加载文件,然后不显示图形

我首先使用read.csv来加载文件,并绘制直方图.然后我改为read_csv.我的直方图现在不起作用了.错误说:

"未知或未初始化的专栏:'First.Income'."

这是我的代码:

library(tidyverse)

mydf<- read_csv("households.csv")

hist(mydf$First.Income,main="Histogram of First Income")
Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
1090
查看次数

如何将 Rmd 文件的 html 输出对齐到左侧

当我将 Rmd 文件编织为 html 时,我在浏览器上可视化的输出始终居中。我怎样才能证明左边的所有东西都是合理的,这样就不会有浪费的空间?浪费的空间是指 TOC 左侧有未使用的空间

编辑我知道这个线程,但我想保留 TOC,我真正想要的是将所有内容都向左移动,这样 TOC 就在左对齐的旁边

代码:

---                                                                        
title: "Example SO"                                                        
output:                                                                    
  html_document:                                                           
    toc: true                                                              
    number_sections: true                                                  
    code_folding: "hide"                                                   
    toc_float:                                                             
      collapse: false                                                      
      smooth_scroll: false                                                 
---                                                                        

```{r setup, echo=FALSE}                                                   
knitr::opts_chunk$set(error = TRUE,                                        
                      echo = TRUE,                                         
                      message = FALSE,                                     
                      comment = "",                                        
                      fig.align = "left"                                   
                      )                                                    
```                                                                        

# H1 Stuff                                                                 

```{r, DT}                                                                 
    DT::datatable(mtcars, rownames = FALSE, options = list(pageLength = 2))
```                                                                        

# H2 More stuff                                                            
Bla 
Run Code Online (Sandbox Code Playgroud)

输出: 输出

r r-markdown dt

2
推荐指数
1
解决办法
933
查看次数

将函数应用于跨列的 pandas DataFrame 以创建用于排序的临时列

基于具有列值函数的 Sort pandas DataFrame

我想使用log()该方法应用一个函数,例如数据框.assign(),以创建临时列并将其用作排序标准,但是,我无法像该方法的工作方式那样传递轴参数.apply()

这是示例代码:

from numpy.random import randint

set.seed(0)
df = pd.DataFrame({'value':[randint(1,10) for i in range(0,10)], 'reading': [randint(1,10) for i in range(0,10)]})
Run Code Online (Sandbox Code Playgroud)
   value  reading
0      8        6
1      5        9
2      3        7
3      8        2
4      6        1
5      4        9
6      6        2
7      3        5
8      2        2
9      8        8
Run Code Online (Sandbox Code Playgroud)

我不能像这样使用 .assign() 方法:

df.assign(log = log(df.value/df.reading))

    raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

0
推荐指数
1
解决办法
2180
查看次数