小编Slo*_*ner的帖子

R ggplot2:着色步骤图取决于值

如何配置ggplot2步骤图,以便当绘制的值超过某个级别时,它是一种颜色,当它低于某一级别时,它是另一种颜色?(最后我想指定使用的颜色.)

我的第一个想法是,这将是一个简单的问题,只需要我在现有数据框中添加一列,并将此列映射到geom_step()的aes().这是有用的:我得到两种颜色,但它们重叠,如下图所示: geom_step()绘图,颜色重叠

我在过去的几个小时内搜索过SO,发现了许多相似但不完全相同的问题.然而,尽管在不同的层中尝试了各种各样的组合,但我还是无法解决问题.代码如下.任何帮助非常感谢.

require(ggplot2)

tmp <- structure(list(date = structure(c(1325635200, 1325635800, 1325636400, 
1325637000, 1325637600, 1325638200, 1325638800, 1325639400, 1325640000, 
1325640600, 1325641200, 1325641800, 1325642400, 1325643000, 1325643600, 
1325644200, 1325647800, 1325648400, 1325649000, 1325649600, 1325650200, 
1325650800, 1325651400, 1325652000, 1325652600, 1325653200, 1325653800, 
1325654400, 1325655000, 1325655600, 1325656200, 1325656800), tzone = "", tclass = c("POSIXct", 
"POSIXt"), class = c("POSIXct", "POSIXt")), Close = c(739.07, 
739.86, 740.41, 741.21, 740.99, 741.69, 742.64, 741.34, 741.28, 
741.69, 741.6, 741.32, 741.95, 741.86, 741.02, 741.08, 742.08, 
742.88, 743.19, 743.18, 743.78, 743.65, 743.66, 742.78, 743.34, 
742.81, 743.31, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

R:ggplot2 中 geom_rect 的渐变填充

我想在 R 中创建一个类似于下面的图形来显示某个人或公司相对于其同行的排名。分数总是在 1 到 100 之间。

秩

尽管我可以接受任何ggplot解决方案,但在我看来,最好的方法是使用geom_rect然后调整并添加 baptiste 对这个问题的回答中描述的箭头。然而,我发现了一些更简单的东西 -geom_rect使用如下图右侧的指南中所示的渐变正确填充。这应该很容易。我究竟做错了什么?

library(ggplot2)
library(scales)

mydf <- data.frame(id = rep(1, 100), sales = 1:100)

ggplot(mydf) +
    geom_rect(aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = 100, fill = sales)) +
    scale_x_discrete(breaks = 0:2, labels = 0:2) +
    scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red', midpoint = 50) +
    theme_minimal()
Run Code Online (Sandbox Code Playgroud)

geom_rect

r ggplot2

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

使用列表推导和dict进行正则表达式替换

以下Python 3代码循环遍历字符串列表,并使用正则表达式替换每个字符串中的一些文本.

这里的字符串很简单,但在实际情况下它们可能更复杂,数量更多,因此我决定使用re.sub()而不是str.replace().

all = ("this line has no hits",
       "a letter to tom from peter",
       "today bonny went to school",
       "harry made some cake")

for myitem in all:
    newitem = re.sub("harry","sally",myitem)
    newitem = re.sub("tom","jerry",newitem)
    newitem = re.sub("bonny","clyde",newitem)
    print(newitem)
Run Code Online (Sandbox Code Playgroud)

这似乎按预期工作:

>>> this line has no hits
a letter to jerry from peter
today clyde went to school
sally made some cake
>>> 
Run Code Online (Sandbox Code Playgroud)

在现实生活中会有大量的字符串,这会产生一堆乱码.我认为通过在a中定义正则表达式对dict并使用列表理解,可能有更简洁,更Pythonic的方法.所以我尝试了这个:

mydict = {'harry':'sally','tom':'jerry','bonny':'clyde'}

newall = [re.sub(i, mydict[i], j) for i in …
Run Code Online (Sandbox Code Playgroud)

python regex dictionary list-comprehension python-3.x

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

R:如何有条件地更改ggplot facet图中使用的3个变量的值

问:如何在ggplot2 facet图中仅显示三个不同变量中的一个显示的值<0,每个变量的幅度各不相同?

我做了以下方面的情节.

在此输入图像描述

如您所见,y轴上绘制的值对于每个变量都有显着差异,但使用的是scales = "free"解决方案.

我想通过限制比例或将小于零的值设置为零来抑制"profit_margin"方面(底部用蓝色显示)中小于零的值,但我无法弄清楚如何完成此操作.我可以直接删除数据框中的值,但我更希望保持数据不变.我尝试在scale_y_continuous()中使用函数,但无法取得任何进展.

以下是用于生成上述情节的代码:

require(lubridate)
require(reshape2)
require(ggplot2)
require(scales)

## Create dummy time series data
set.seed(12345)
monthsback <- 12
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
                   sales = runif(monthsback, min = 600, max = 800),
                   profit = runif(monthsback, min = -50, max = 80))
## Add calculation based on data
mydf$profit_margin <- mydf$profit/mydf$sales

## Reshape...
mymelt <- melt(mydf, id = c('mydate'))

## Plot
p …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

R:使用 ggplot geom_line 防止显示时间序列数据的断线

使用 ggplot2 我想画一条在特定日期后改变颜色的线。我原以为这会很简单,但在颜色变化的地方我在线条上中断了。最初我认为这是一个问题group(根据这个问题;另一个问题看起来也相关,但不完全是我需要的)。在美学上搞砸了group30 分钟后我无法修复它,所以如果有人能指出明显的错误......

截屏

代码:

require(ggplot2)

set.seed(1111)
mydf <- data.frame(mydate = seq(as.Date('2013-01-01'), by = 'day', length.out = 10),
    y = runif(10, 100, 200))
mydf$cond <- ifelse(mydf$mydate > '2013-01-05', "red", "blue")

ggplot(mydf, aes(x = mydate, y = y, colour = cond)) +
    geom_line() +
    scale_colour_identity(mydf$cond) +
    theme()
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

如何按地区添加颜色?

数据

代码

    #
    # This is code for mapping of CGE_Morocco results
    #

    # rm(list = ls(all = TRUE)) # don't use this in code that others will copy/paste

    ## Loading packages
    library(rgdal)
    library(plyr)
    library(maps)
    library(maptools)
    library(mapdata)
    library(ggplot2)
    library(RColorBrewer)

    ## Loading shape files administrative coordinates for Morocco maps
    #Morocco <- readOGR(dsn=".", layer="Morocco_adm0")
    MoroccoReg <- readOGR(dsn=".", layer="Morocco_adm1")

    ## Reorder the data in the shapefile based on the regional order
    MoroccoReg <- MoroccoReg[order(MoroccoReg$ID_1), ] 

    ## Add the yield impacts column to …
Run Code Online (Sandbox Code Playgroud)

maps r ggplot2

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

用符号替换geom_line的ggplot2图例键

我有三个股票的价格线图,我通过从我正在看的时期开始的百分比变化来规范化.这似乎工作正常,但不是当前构成图例键的灰色背景上的彩色线条,我想要在关键标签旁边的正方形或彩色圆圈.

这在ggplot2中甚至可能吗?任何指针,无论多么简短,都赞赏.生成图表的代码如下.

Date <- c("2011-09-19","2011-09-20","2011-09-21","2011-09-22",
                "2011-09-23","2011-09-26","2011-09-27","2011-09-28","2011-09-29","2011-09-30")
CoA <- c(100,100,95,93,88,91,98,109,115,106)
CoB <- c(16.5,16.8,17.2,17,17.5,16.5,16,15.5,16.1,16.3)
CoC <- c(3.2,3.18,3.15,3.12,3.15,3.1,3.08,3.11,3.35,3.42)
prices <- data.frame(Date,CoA,CoB,CoC)
changes <- as.data.frame(matrix(nrow=nrow(prices),ncol=ncol(prices)))
changes[,1]=prices[,1]
for(i in 2:ncol(prices)){ # calculate changes in price
    changes[,i]= (prices[,i]-prices[,i][1])/prices[,i][1]
}
colnames(changes) <- colnames(prices)
changes <- melt(changes, id = "Date")
changes$Date <- as.Date(as.character(changes$Date))
chart1 <- ggplot(data=changes,aes(x=changes$Date,y=changes$value,colour=changes$variable))
chart1 <- chart1 + geom_line(lwd=0.5) + ylab("Change in price (%)") + xlab("Date") +
    labs(colour="Company")
print(chart1)
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

使用 ggplot 扩展 y 轴

我从美国联邦储备银行网站上获得了一些利率数据,我正在尝试绘制收益率曲线。我打算用它来与其他一些人进行比较并随着时间的推移保持一致,我希望尽可能长时间地在尽可能多的国家/地区保持 y 轴上的相同轴范围。以下代码scale_y_continuous(limits=c(0,7))ylim(0,7)两者都给出错误。有没有人对我可能做错的事情有任何想法?谢谢

library(reshape2)
library(data.tool)
library(ggplot2)

    x =    structure(list(Series.Description = c("2012-07-27", "2012-10-26"
        ), `1.month` = c("0.08", "0.12"), `6.month` = c("0.15", "0.15"
        ), `1.year` = c("0.17", "0.19"), `2.year` = c("0.23", "0.30"), 
            `5.year` = c("0.59", "0.78"), `10.year` = c("1.47", "1.81"
            ), `30.year` = c("2.51", "2.94")), .Names = c("Series.Description", 
        "1.month", "6.month", "1.year", "2.year", "5.year", "10.year", 
        "30.year"), row.names = c(1L, 4L), class = "data.frame")


    # dates as # of days
    z=c(30,182,365,730,1825,3650,10950)
    names(x)[1]="date"
    names(x)[-1]=c(30,182,365,730,1825,3650,10950)
    x=melt(x,id.vars=c(1))
    x$variable=levels(x$variable)[x$variable]
       x$variable=as.numeric(x$variable)

    ggplot(data=x,aes(x=variable,y=value,group=date,linetype=date)) + 
      geom_line(colour="red") + …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

标签 统计

ggplot2 ×7

r ×7

dictionary ×1

list-comprehension ×1

maps ×1

python ×1

python-3.x ×1

regex ×1