我一直试图从这里扩展我的场景以利用facets(特别是facet_grid()
).
我已经看过这个例子,但是我似乎无法让它为我geom_bar()
和geom_point()
组合工作.我试图从仅仅改变例子中使用的代码facet_wrap
来facet_grid
这也似乎让第一层不显示.
对于网格和凹凸,我是一个非常新手,所以如果有人可以提供一些指导,如何使P1显示左侧y轴,P2显示在右侧y轴上,这将是很好的.
数据
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
grid.newpage()
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=c("clarity","cut")]
setkey(d1, clarity,cut)
Run Code Online (Sandbox Code Playgroud)
p1和p2
p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill=cut)) +
geom_bar(stat="identity") +
labs(x="clarity", y="revenue") +
facet_grid(. ~ cut) +
scale_y_continuous(labels=dollar, expand=c(0,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(colour="#4B92DB"),
legend.position="bottom")
p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
geom_point(size=6) +
labs(x="", y="number …
Run Code Online (Sandbox Code Playgroud) 我试图计算多个列的中值,但我的数据有点时髦.它看起来像下面的例子.
library(data.table)
dt <- data.table("ID" = c(1,2,3,4),"none" = c(0,5,5,3),
"ten" = c(3,2,5,4),"twenty" = c(0,2,3,1))
ID none ten twenty
1: 1 0 3 0
2: 2 5 2 2
3: 3 5 5 3
4: 4 3 4 1
Run Code Online (Sandbox Code Playgroud)
在表中,列表示该值的出现次数.我想计算中位数.
例如,对于ID = 1
median(c(10, 10, 10))
Run Code Online (Sandbox Code Playgroud)
是我想要创建的计算.
对于ID = 2
median(c(0, 0, 0, 0, 0, 10, 10, 20, 20))
Run Code Online (Sandbox Code Playgroud)
我尝试过使用rep()
并lapply()
取得了非常有限的成功,并且已经就如何实现这一目标提供了明确的指导.我理解为喜欢rep()
我将不得不硬编码我的价值重复(例如rep(0,2)
或rep(10,2)
),这是我所期望的.我正在努力创建一个包含每列重复的列表或向量.
我正在尝试创建一个rmarkdown文档.我终于找到了解决这个问题的方法,尽管已经花了很长时间.我希望能做的最后一件事是将图像添加到我的pdf文档的标题页.
我遇到的麻烦是我的标题页是由YAML的顶部定义的.以下是我的example.Rmd
文件的内容.我使用RStudio中的Knit PDF按钮将其转换为PDF.
---
title: "This is a my document"
author: "Prepared by: Dan Wilson"
date: '`r paste("Date:",Sys.Date())`'
mainfont: Roboto Light
fontsize: 12pt
documentclass: report
output:
pdf_document:
latex_engine: xelatex
highlight: tango
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggplot构建双y轴图表.首先,请允许我说,我不是在讨论是否这样做是好的做法的优点.在查看基于时间的数据以确定2个离散变量的趋势时,我发现它们特别有用.在我看来,对此的进一步讨论更适合交叉验证.
Kohske提供了一个如何做到这一点的非常好的例子,到目前为止我已经习惯了很好的效果.然而,我无法在两个y轴上包含一个图例.我在这里和这里也看到过类似的问题,但似乎没有人能解决包含传奇的问题.
我有一个可重现的例子,使用ggplot中的钻石数据集.
数据
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
grid.newpage()
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=clarity]
setkey(d1, clarity)
Run Code Online (Sandbox Code Playgroud)
图表
p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill="#4B92DB")) +
geom_bar(stat="identity") +
labs(x="clarity", y="revenue") +
scale_fill_identity(name="", guide="legend", labels=c("Revenue")) +
scale_y_continuous(labels=dollar, expand=c(0,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(colour="#4B92DB"),
legend.position="bottom")
p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
geom_point(size=6) +
labs(x="", y="number of stones") + expand_limits(y=0) +
scale_y_continuous(labels=comma, expand=c(0,0)) +
scale_colour_manual(name …
Run Code Online (Sandbox Code Playgroud) 我有一个如下数据集:
library(data.table)
dt1 <- data.table(urn = c(rep("a", 5), rep("b", 4)),
amount = c(10, 12, 23, 15, 19, 42, 11, 5, 10),
date = as.Date(c("2016-01-01", "2017-01-02", "2017-02-04",
"2017-04-19", "2018-02-11", "2016-02-14",
"2017-05-06", "2017-05-12", "2017-12-12")))
dt1
# urn amount date
# 1: a 10 2016-01-01
# 2: a 12 2017-01-02
# 3: a 23 2017-02-04
# 4: a 15 2017-04-19
# 5: a 19 2018-02-11
# 6: b 42 2016-02-14
# 7: b 11 2017-05-06
# 8: b 5 2017-05-12
# 9: b …
Run Code Online (Sandbox Code Playgroud) 我有一个data.table,我想要聚合
library(data.table)
dt1 <- data.table(year=c("2001","2001","2001","2002","2002","2002","2002"),
group=c("a","a","b","a","a","b","b"),
amt=c(20,40,20,35,30,28,19))
Run Code Online (Sandbox Code Playgroud)
我希望sum
按年份和组进行分析,然后筛选出任何给定组的总和amt大于100的情况.
我已经获得了data.table总和.
dt1[, sum(amt),by=list(year,group)]
year group V1
1: 2001 a 60
2: 2001 b 20
3: 2002 a 65
4: 2002 b 47
Run Code Online (Sandbox Code Playgroud)
我的最终过滤级别遇到了问题.
我正在寻找的最终结果是:
year group V1
1: 2001 a 60
2: 2002 a 65
Run Code Online (Sandbox Code Playgroud)
作为a) 60 + 65 > 100
而b) 20 + 47 <= 100
任何关于如何实现这一点的想法都会很棒.
我看了一下这个data.table总和的组和返回行的最大值,并想知道他们是否是一个同样雄辩的解决我的问题的方法.
我正在使用R/shinydasboard创建一个我正在登录屏幕后面的Web应用程序.
我无法根据侧边栏菜单标签获取主体.
我试图确保其中一个标签项目selected = TRUE
仍无济于事.
示例代码如下:
require(shiny)
require(shinydashboard)
Logged <- FALSE;
LoginPass <- 0; #0: not attempted, -1: failed, 1: passed
login <- box(title = "Login",textInput("userName", "Username (user)"),
passwordInput("passwd", "Password (test)"),
br(),actionButton("Login", "Log in"))
loginfail <- box(title = "Login",textInput("userName", "Username"),
passwordInput("passwd", "Password"),
p("Username or password incorrect"),
br(),actionButton("Login", "Log in"))
mainbody <- div(tabItems(
tabItem(tabName = "t_item1", box(title = "Item 1 information")),
tabItem(tabName = "t_item2", box(title = "Item 2 information")),
tabItem(tabName = "t_item3", box(title = "Item 3 information"))
)
) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试替换我的一组特定列的NA值tibble
.列都以相同的前缀开头,所以我想知道是否有一种简洁的方法来使用包中的starts_with()
函数来dplyr
允许我这样做.
我在SO上看到了其他几个问题,但是它们都需要使用特定的列名或位置.我真的想要懒惰,不想定义所有列,只是前缀.
我replace_na()
从tidyr
包中尝试了这个功能无济于事.我知道我的代码对于作业是错误的,但我的词汇量不够大,不知道在哪里看.
Reprex:
library(tidyverse)
tbl1 <- tibble(
id = c(1, 2, 3),
num_a = c(1, NA, 4),
num_b = c(NA, 99, 100),
col_c = c("d", "e", NA)
)
replace_na(tbl1, list(starts_with("num_") = 0)))
Run Code Online (Sandbox Code Playgroud) 在尝试了对RStudio Shiny Pro服务器的评估之后,我并不十分热衷于登录/身份验证机制,因为它们不是简单的机制来管理用户帐户以便客户端访问闪亮的应用程序.
因此,我试图在Shiny中创建自己的登录机制,除了shinydashboard
框架内的事物显示之外,所有意图和目的都正常工作.在显示所有内容之前,事情似乎已切断.我的登录代码对https://gist.github.com/withr/9001831略有不同,所以非常感谢.
我的代码:
require(shiny)
require(shinydashboard)
my_username <- "test"
my_password <- "abc"
header <- dashboardHeader(title = "my heading")
sidebar <- uiOutput("sidebarpanel")
body <- uiOutput("body")
login <- box(title = "Login",textInput("userName", "Username"),
passwordInput("passwd", "Password"),
br(),actionButton("Login", "Log in"))
mainpage <- "some data"
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output, session) {
USER <<- reactiveValues(Logged = Logged)
observe({
if (USER$Logged == FALSE) {
if (!is.null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd) …
Run Code Online (Sandbox Code Playgroud) 我有一个data.table,我想总结一下.这是我的方法
library(data.table)
dtIris <-data.table(iris)
dt1 <- dtIris[, list(AvgSepalWidth = mean(Sepal.Width)),
by=list(TrimSpecies = substr(Species,1,3),Petal.Length)]
Run Code Online (Sandbox Code Playgroud)
我希望能够使用变量来识别要分组的项目之一,我只是无法让它来评估列表中的变量.它只是将它视为一个字符串并抛出错误.
myvar <- "Petal.Length"
dt1 <- dtIris[, list(AvgSepalWidth = mean(Sepal.Width)),
by=list(TrimSpecies = substr(Species,1,3),myvar)]
Run Code Online (Sandbox Code Playgroud)
我已经试过noquote()
,eval()
,parse(text=)
都无济于事.任何指导都会非常感激.