小编shi*_*iny的帖子

订阅和上标" - "或"+"与ggplot2轴标签?(离子化学符号)

我使用下面的代码得到了这个情节

在此输入图像描述

在我的情节中,我希望NO3有负号" - "作为上标,如下所示

在此输入图像描述

在x轴的标签中,我不能仅使用负号作为NO3的上标,所以我必须使用-1,如下所示

x <- seq(0,2*pi,0.1)
y <- sin(x)

df <- data.frame(x, y)
ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{-1}-N~Kg^{-1}),
     y=expression(Concentration~mg~L^{-1})) 
Run Code Online (Sandbox Code Playgroud)

关于如何更改标签只有没有1的负号的任何建议?

r notation ggplot2

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

ggplot2:在图中添加p值

我有这个情节

在此输入图像描述

使用下面的代码

library(dplyr) 
library(ggplot2)
library(ggpmisc)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))

formula <- y ~ x    
ggplot(df, aes(x= new_price, y= carat, color = cut)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~clarity, scales = "free_y") +
  geom_smooth(method = "lm", formula = formula, se = F) +
  stat_poly_eq(aes(label = paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.15,
               formula = formula, parse = TRUE, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 p-value

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

dplyr::mutate(分配 na.rm =TRUE)

我有一个包含 100 个变量的 data.frame。我只想使用mutate(not summarise)获得三个变量的总和。

如果 3 个变量中的任何一个中有 NA,我仍然想获得sum. 为了做到这一点mutate,我NA0using替换了所有值,ifelse然后我得到了sum.

library(dplyr)
df %>% mutate(mod_var1 = ifelse(is.na(var1), 0, var1),
              mod_var2 = ifelse(is.na(var2), 0, var2),
              mod_var3 = ifelse(is.na(var3), 0, var3),
              sum = (mod_var1+mod_var2+mod_var3))
Run Code Online (Sandbox Code Playgroud)

有没有更好(更短)的方法来做到这一点?

数据

df <- read.table(text = c("
var1    var2    var3
4   5   NA
2   NA  3
1   2   4
NA  3   5
3   NA  2
1   1   5"), header =T)
Run Code Online (Sandbox Code Playgroud)

r sum na dplyr

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

使用表达式()将左边的两个线条图例标题对齐

使用此脚本

df <- data.frame(x = 1:5, y = 1:5, color = letters[1:5])
ggplot(df, aes(x, y, fill = color)) +
  geom_point(size = 4, pch = 21) + 
  guides(fill = guide_legend(
     title = expression(atop(Median~Nitrate-Nitrogen~(NO[3]^{textstyle("-")}-N), ".\n Concentration"~(mg~L^{textstyle("-")})))))
Run Code Online (Sandbox Code Playgroud)

我有这个数字

在此输入图像描述

有关如何将图例标题的第二行与左侧对齐的任何建议吗?

r legend ggplot2

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

ggplot2:在每个方面重新排序从最高到最低的条形

df下面,我想在每个方面重新排序从最高到最低的条形

我试过了

df <- df %>% tidyr::gather("var", "value", 2:4)
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity")+facet_wrap(~var, ncol =3)
Run Code Online (Sandbox Code Playgroud)

它给了我 在此输入图像描述

它没有在每个方面从最高到最低排序.

我想出了另一种获得我想要的方法.我不得不一次绘制每个变量,然后使用组合所有绘图grid.arrange()

#I got this function from @eipi10's answer
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937  
#Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot) {
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity") …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-bar

6
推荐指数
2
解决办法
3525
查看次数

如何将Shiny应用程序中的表格和绘图作为参数传递给R Markdown?

在此 Shiny 应用程序中,用户可以上传 .csv 文件,以表格和绘图的形式获取结果。我希望能够将结果下载为 PDF 文档。

输入文件

#I created the input .csv file to be used in the app from diamonds data.frame
library(ggplot2)
df <-  diamonds[1:5000, ]
head(df)
write.csv(df, "df.csv")
Run Code Online (Sandbox Code Playgroud)

应用程序

library(tidyverse)
library(shiny)
library(rmarkdown)
library(knitr)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(fileInput("file","Upload your file"), 
                 width =2),
    mainPanel(
      width = 10,
      downloadButton("report", "Download report"),
      tableOutput("table"),
      tags$br(),
      tags$hr(),
      plotOutput("plot1"), 
      tags$br(),
      tags$hr(),
      plotOutput("plot2")
    )
  )
)

server <- function(input,output){

  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.csv(file1$datapath, header=TRUE, sep=',')
    })


  output$table <- renderTable({
    if …
Run Code Online (Sandbox Code Playgroud)

r r-markdown shiny reactive

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

在ggplot2中创建特定的日期范围(scale_x_date)

嗨,我有从2010年到2050年的年度数据.我正在尝试制作线图,以便我的x轴从2010年开始到2050年结束,显示5年间隔iei希望x轴断裂为(2010年,2015年,2020年......) .,2045,2050).不幸的是,它从2013年开始到2048年结束.为什么?怎么解决这个问题?我的数据,代码和结果图片如下.

plot1<- ggplot(test, aes(yr, y=value)) +
geom_line()+
scale_x_date(breaks = date_breaks("5 year"),labels=date_format("%Y")) +
scale_y_continuous(breaks=seq(-4,6, by=1))
plot1
Run Code Online (Sandbox Code Playgroud)

数据

 dput(test)
structure(list(value = c(2.47099989218436, 3.09640452148661, 
1.32121989082519, 0.742309399667898, 0.180070229458727, 2.2475619117108, 
0.606470664265897, 2.12742331755353, 2.73894680327422, 2.22368873465667, 
1.58381022102847, 2.10136510397371, 1.74582199030396, 2.21689521610787, 
2.51618709804907, 1.87243814589322, 1.92029935267449, 1.79383249007138, 
0.537680017904451, 1.2415782984683, 3.62075008273724, 4.50975793125965, 
3.70660640492563, 4.16317150909305, 2.24008439109747, 2.24587596633027, 
3.63019754286973, 4.28513572439197, 3.61013179034863, 4.20010027834161, 
2.06766292535187, 4.34833637648799, 5.71460894423653, 4.12185659615561, 
3.93305702163007, 3.29384139246081, 3.2915580598453, 4.21009646693621, 
4.32889796119913, 4.99213117815761), yr = structure(c(14610, 
14975, 15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897, 
18262, 18628, 18993, 19358, 19723, 20089, 20454, 20819, …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 timeserieschart

4
推荐指数
1
解决办法
6613
查看次数

dplyr:年末和开始年份之间的价值差异

用这个 data.frame

siteID <- c(rep("site1", 16), rep("site2", 16), rep("site3", 16),rep("site4", 16))
YEAR <-   rep(c("2003", "2004", "2005", "2006"), 16)
parameter <- c(rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
               rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
               rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4),
               rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4))
value <- c(seq(1, 4, by=1), seq(10, 40, by=10), seq(12, 18, by=2), seq(5, 20, by=5),
           seq(3, 12, by=3), sample(13:18, 4), sample(15:22, 4), sample(10:18, 4),
           seq(7, 1, by=-2), sample(15:22, 4), …
Run Code Online (Sandbox Code Playgroud)

r dplyr

4
推荐指数
1
解决办法
291
查看次数

ggplot2:添加回归方程和R2并调整它们在图上的位置

使用df和下面的代码

library(dplyr) 
library(ggplot2)
library(devtools)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))

ggplot(df, aes(x= new_price, y= carat, color = cut))+
  geom_point(alpha = 0.3)+
  facet_wrap(~clarity, scales = "free_y")+
  geom_smooth(method = "lm", se = F)
Run Code Online (Sandbox Code Playgroud)

我有这个情节

在此输入图像描述

感谢@ kdauria对这个问题的回答,我将回归方程和R2添加到如下图中

source_gist("524eade46135f6348140")
ggplot(df, aes(x= new_price, y= carat, color = cut))+
  stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE)+
  geom_point(alpha = 0.3)+
  facet_wrap(~clarity, scales = "free_y")+
  geom_smooth(method = "lm", se …
Run Code Online (Sandbox Code Playgroud)

regression r facet ggplot2

4
推荐指数
1
解决办法
7799
查看次数

有条件地使用replace_na

我希望有条件地使用tidyverse取代截至2017年7月16日的缺失收入.

我的数据

library(tidyverse)
library(lubridate)

    df<- tribble(
                 ~Date, ~Revenue,
          "2017-07-01",      500,
          "2017-07-02",      501,
          "2017-07-03",      502,
          "2017-07-04",      503,
          "2017-07-05",      504,
          "2017-07-06",      505,
          "2017-07-07",      506,
          "2017-07-08",      507,
          "2017-07-09",      508,
          "2017-07-10",      509,
          "2017-07-11",      510,
          "2017-07-12",      NA,
          "2017-07-13",      NA,
          "2017-07-14",      NA,
          "2017-07-15",      NA,
          "2017-07-16",      NA,
          "2017-07-17",      NA,
          "2017-07-18",      NA,
          "2017-07-19",      NA,
          "2017-07-20",      NA
          )

df$Date <- ymd(df$Date)
Run Code Online (Sandbox Code Playgroud)

我想要有条件地替换NA的日期

max.date <- ymd("2017-07-16")
Run Code Online (Sandbox Code Playgroud)

输出我想要的

    # A tibble: 20 × 2
             Date Revenue
            <chr>   <dbl>
    1  2017-07-01     500
    2  2017-07-02     501
    3  2017-07-03     502
    4  2017-07-04     503
    5 …
Run Code Online (Sandbox Code Playgroud)

r missing-data tidyverse

4
推荐指数
1
解决办法
501
查看次数