小编ter*_*don的帖子

如何更改ggplot2密度图中的颜色?

简介:我想为ggplot2()密度分布图选择颜色而不会丢失自动生成的图例.

细节:我有一个使用以下代码创建的数据框(我意识到它不优雅,但我只是在学习R):

cands<-scan("human.i.cands.degnums")
non<-scan("human.i.non.degnums")
df<-data.frame(grp=factor(c(rep("1. Candidates", each=length(cands)),
           rep("2. NonCands",each=length(non)))), val=c(cands,non))
Run Code Online (Sandbox Code Playgroud)

然后我绘制他们的密度分布如下:

library(ggplot2)
ggplot(df, aes(x=val,color=grp)) + geom_density() 
Run Code Online (Sandbox Code Playgroud)

这会产生以下输出:

在此输入图像描述

我想选择线条出现的颜色,不能为我的生活弄清楚如何.我在网站上阅读了其他各种帖子,但无济于事.最相关的是:

经过一段时间的搜索,我尝试过:

## This one gives an error
ggplot(df, aes(x=val,colour=c("red","blue"))) + geom_density() 
Error: Aesthetics must either be length one, or the same length as the dataProblems:c("red", "blue")

## This one produces a single, black line
ggplot(df, aes(x=val),colour=c("red","green")) + geom_density() 
Run Code Online (Sandbox Code Playgroud)

我想出的最好的是:

ggplot() + geom_density(aes(x=cands),colour="blue")  + geom_density(aes(x=non),colour="red")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 正如您在上图中看到的那样,最后一个命令正确地更改了线条的颜色,它删除了图例.我喜欢 ggplot2的传奇系统.它很好很简单,我不想在重新创建ggplot显然能够做的事情.最重要的是,语法非常难看.我的实际数据框由7组不同的数据组成.我不敢相信写+ geom_density(aes(x=FOO),colour="BAR")7次是最优雅的编码方式.

所以,如果一切都失败了,我会接受一个答案,告诉我如何让传奇回到第二个情节.但是,如果有人能告诉我如何正确地做到这一点,我将非常高兴.

plot r ggplot2

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

Perl子例程如何报告调用它的行?

我正在编写一个Perl管道,一个调用各种其他程序的脚本,并管理从一个到另一个的数据传递.脚本(称之为pipeline.pl)和它管理的各种子脚本共享一个subroutines.ph通过require subroutines.ph指令定义和包含的公共子程序列表.

其中一个是一个函数,其工作是退出打印错误消息(实际的子例程也做了一些其他工作,但它们在这里没有关系;不,我不是重新发明die()):

## subroutines.ph
sub errorDie
{
    my ($errMsg) = @_;
    ## various other cleanup tasks here
    die($errMsg);
}
1;
Run Code Online (Sandbox Code Playgroud)

并且,在pipeline.pl:

#!/usr/bin/perl 
require 'subroutines.ph';

errorDie("foo")
Run Code Online (Sandbox Code Playgroud)

运行上面的脚本会导致:

foo at subroutines.ph line 5.
Run Code Online (Sandbox Code Playgroud)

是否可以让它报告类似于:

foo at pipelines.pl line 4.
Run Code Online (Sandbox Code Playgroud)

因此,die()它不应报告找到的行,而应报告errorDie调用子例程的原始脚本行.我知道我可以通过在$errMsg变量中包含行来实现这一点,但这很脆弱且很麻烦.这可以自动完成吗?外部文件中定义的子程序能否检测到它的调用位置?

perl error-reporting die subroutine

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

如何使编译器警告这是无效布尔值?

我们只是被错字弄糊涂了:“ constexpr bool maxDistance = 10000;

gcc和clang都编译此文件而没有任何警告。

真正的错误是该变量不应为bool类型,而应为整数类型。

我们如何确保将来收到编译器警告?

#include <iostream>

constexpr bool number = 1234;
int main(int argc, char* argv[])
{
    std::cout << number + 10000 << std::endl; // prints 10001.
    return number;
}
Run Code Online (Sandbox Code Playgroud)

这里的错误是变量声明的类型错误,但是clang和gcc均未给出警告。

gcc -Wall -std=c++14 test.cpp -lstdc++
clang -Wall -std=c++14 test.cpp -lstdc++
Run Code Online (Sandbox Code Playgroud)

(使用gcc 5.4.0和clang 3.8.0)

注意:此后,我已经了解了一个可能的编译标志:-Wint-in-bool-context但是,这似乎在我使用的版本(5.4.0)和clang(3.8.0)中均未实现。

这是正确的方法吗?

c++ boolean compiler-warnings constexpr

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

我需要制作一个 awk 脚本来解析文件中的文本。我不确定我是否做对了

你好,我需要制作一个 awk 脚本来解析 csv 文件并在 bash 中对其进行排序。我需要从维基百科获取总统列表,并按年份对他们的任职年份进行排序。当一切都整理好后,每只耳朵都需要放在一个文本文件中。我不确定我做得是否正确

这是我的 csv 文件的一部分:

28,Woodrow Wilson,http:..en.wikipedia.org.wiki.Woodrow_Wilson,4.03.1913,4.03.1921,Democratic ,WoodrowWilson.gif,thmb_WoodrowWilson.gif,New Jersey
29,Warren G. Harding,http:..en.wikipedia.org.wiki.Warren_G._Harding,4.03.1921,2.8.1923,Republican ,WarrenGHarding.gif,thmb_WarrenGHarding.gif,Ohio 
Run Code Online (Sandbox Code Playgroud)

我想包括 2 美元,我认为是名字,并按 4 美元排序,这是总统就任的日期

这是我实际的 awk 文件:

#!/usr/bin/awk -f
 -F, '{
if (substr($4,length($4)-3,2) == "17")
 { print $2 > Presidents1700 }
else if (substr($4,length($4)-3,2) == "18")
{ print $2 > Presidents1800 }
else if (substr($4,length($4)-3,2) == "19")
{ print $2 > Presidents1900 }
else if (substr($4,length($4)-3,2) == "20")
{ print $2 > Presidents2000 }
}' 
Run Code Online (Sandbox Code Playgroud)

这是我运行它的函数:

SplitFile() {                                                                              
printf "Task 4: …
Run Code Online (Sandbox Code Playgroud)

linux bash awk command-line-interface

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