小编Sti*_*ibu的帖子

在R中按月汇总行数

所以我有一个数据框,它有一个日期列,一个小时列和一系列其他数字列.数据框中的每一行是一整天的1小时1天.

数据框如下所示:

          Date  Hour  Melbourne  Southern  Flagstaff
1   2009-05-01     0          0         5         17
2   2009-05-01     2          0         2          1
3   2009-05-01     1          0        11          0
4   2009-05-01     3          0         3          8
5   2009-05-01     4          0         1          0
6   2009-05-01     5          0        49         79
7   2009-05-01     6          0       425        610
Run Code Online (Sandbox Code Playgroud)

小时数无序,因为这是从另一个数据框中的子集.

我想按月和可能按天将数值中的值相加.有谁知道我怎么做到这一点?

aggregate r dataframe lubridate dplyr

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

如何在ggplot2中的x轴下添加注释?

我有以下图表:

library(ggplot2)
library(scales)
library(magrittr)
df1 <-
  structure(
    list(
      x = structure(
        1:5, .Label = c("5", "4", "3", "2",
                        "1"), class = "factor"
      ), y = c(
        0.166666666666667, 0.361111111111111,
        0.0833333333333333, 0.222222222222222, 0.291666666666667
      )
    ), .Names = c("x",
                  "y"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE
  )

df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") +
  scale_y_continuous(labels = percent) 
Run Code Online (Sandbox Code Playgroud)

我想在5和1之下添加带有粗体文本的两行注释.例如,'最高\nvalue'低于5,'最低\n值'低于1.

我试过geom_text但我不能把文字放在我想要的地方.

r ggplot2

10
推荐指数
2
解决办法
9983
查看次数

与geom_bar有关的问题?

自从David Robinson发布他的gganimate包并认为我自己有一个游戏以来,我一直羡慕和钦佩在Twitter上出现的各种ggplot动画.使用geom_bar时,我遇到了gganimate的问题.希望以下示例演示此问题.

首先为可重现的示例生成一些数据:

df <- data.frame(x = c(1, 2, 1, 2),
                 y = c(1, 2, 3, 4),
                 z = c("A", "A", "B", "B"))
Run Code Online (Sandbox Code Playgroud)

为了证明我正在尝试做什么,我认为绘制一个普通的ggplot是有用的z.我正试图让gganimate产生一个在这两个图之间循环的gif.

ggplot(df, aes(x = x, y = y)) +
   geom_bar(stat = "Identity") +
   facet_grid(~z)
Run Code Online (Sandbox Code Playgroud)

facetted_barchart

但是当我使用gganimate时,B的情节表现得很奇怪.在第二帧中,条形从第一帧条形结束的值开始,而不是从原点开始.好像它是一个堆积的条形图.

p <- ggplot(df, aes(x = x, y = y, frame = z)) +
   geom_bar(stat = "Identity") 
gg_animate(p)
Run Code Online (Sandbox Code Playgroud)

bars_animation

顺便说一下,当尝试相同的情节时,geom_point一切都按预期工作.

q <- ggplot(df, aes(x = x, y = y, frame = z)) +
    geom_point() 
gg_animate(q)
Run Code Online (Sandbox Code Playgroud)

我试图发布一些图片,但显然我没有足够的声誉,所以我希望没有它们就有意义.这是一个错误,还是我错过了什么?

提前致谢,

托马斯

r ggplot2 gganimate

8
推荐指数
1
解决办法
1606
查看次数

迭代数据帧并选择空值

我试图遍历一个具有列= [myCol]的空值的数据帧.我可以很好地遍历数据帧,但是当我指定我只想看到空值时,我得到一个错误.

最终目标是我想强制一个值进入Null字段,这就是为什么我要迭代来识别哪个是第一个.

for index,row in df.iterrows():
    if(row['myCol'].isnull()):
        print('true')

AttributeError: 'str' object has no attribute 'isnull'
Run Code Online (Sandbox Code Playgroud)

我尝试指定column ='None',因为这是我在打印数据帧迭代时看到的值.仍然没有运气:

for index,row in df.iterrows():
    if(row['myCol']=='None'):
        print('true')

No returned rows
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢!

python null dataframe

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

如何缩放x轴并在R中添加刻度

我使用geom_histogram在R中构建直方图,我想将我的x轴缩放到显示的252个insted 252并且看到一个刻度evrey 5数字(0,5,10等),我使用了scale_x_continuous(breaks=(0,155,5).它工作但直方图并未在整个屏幕上显示.我用xlim(0,155)它在整个屏幕上显示直方图,但它覆盖了我定义的刻度.

axis r ggplot2

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

避免消息" - - 从.sqliterc加载资源"

小问题,但令人恼火:有没有办法避免每次进行查询时出现以下消息:

- 从/Users/ThG/.sqliterc加载资源

sqlite

5
推荐指数
3
解决办法
2321
查看次数

条形图中条形之间的特定空格 - ggplot2 - R.

我有一个简单的条形图,如下所示

a<-data.frame(x=c("total","male","female","low education",
            "mid education","high education","working","not working"),
        y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))


ggplot(a,aes(x,y)) + 
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 现在,因为x轴的水平(翻转,现在看起来像是y)彼此有关系,例如男性和女性代表性别崩溃,工作和不工作代表另一个故障等,我希望轴之间留下一些空间每次细分以指出这些故障.

我已经尝试了一些东西scale_x_discrete和它的参数中断,但似乎这不是它的方式.有任何想法吗 ?

r ggplot2 geom-bar

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

vline左侧的阴影密度图?

是否可以使用 vline 作为截止点对密度图进行着色?例如:

df.plot <- data.frame(density=rnorm(100))
library(ggplot2)
ggplot(df.plot, aes(density)) + geom_density() + 
  geom_vline(xintercept = -0.25)
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个新变量,但它没有按我预期的那样工作

df.plot <- df.plot %>% mutate(color=ifelse(density<(-0.25),"red","NULL"))


ggplot(df.plot, aes(density, fill = color, colour = color)) + geom_density() + 
  geom_vline(xintercept = -0.25)
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

极柱图,最内圈为空 使用 R

我想要一个圆形条形图,如下所示:

在此处输入图片说明 但现在,我只有: 在此处输入图片说明

为此,我在 R 中使用了以下代码:

require(ggplot2)
ggplot(PolarPlot,aes(x,y,
                     fill=x))+
      geom_bar(width=1,stat="identity")+
      coord_polar() + xlab("")+ylab("")+
  theme(legend.position = "none" , axis.text.y = element_blank() ,
        axis.ticks = element_blank()
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我需要进行哪些修改才能获得所需的图表吗?

数据如下:

PolarPlot <- structure(list(x = structure(1:7, .Label = c("Class1", "Class2", 
    "Class3", "Class4", "Class5", "Class6", "Class7"), class = "factor"), 
    y = c(2L, 8L, 17L, 56L, 28L, 7L, 2L)), .Names = c("x", "y"),
    class = "data.frame", row.names = c(NA, -7L))
Run Code Online (Sandbox Code Playgroud)

plot r bar-chart ggplot2

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

熊猫情节线具有不同的线条样式?

我正在使用该ggplot样式在同一个图上绘制多条线。采用这种风格,线条就变成了全实线。所以能见度不好。如何将每条线更改为不同的样式,例如带有虚线的样式或其他样式?

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')

fig,ax = plt.subplots(figsize=(15,5))
ax.set_title('Loss curve', fontsize=15)
ax.set_ylabel('Loss')
ax.set_xlabel('Epoch')
df1.plot.line(ax=ax,x='epoch',y=["train_loss"])
df2.plot.line(ax=ax,x='epoch',y=["train_loss"])
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

python matplotlib pandas

3
推荐指数
1
解决办法
7931
查看次数