2015-02-04 79 views
6

Tôi muốn tạo biểu đồ nơi màu tô thay đổi tùy thuộc vào đầu thấp của thùng. Tôi không muốn điền số manual. Điều này answer có vẻ đầy hứa hẹn, nhưng tôi không thể chuyển đổi nó thành công thành biểu đồ màu và hai giá trị (không tô màu). Tôi tin rằng giải pháp có thể là một số logic ifelse trong phạm vi geom_histogram(fill=) nhưng tôi không biết cách truy cập giá trị bắt đầu bin.Biểu đồ tô màu có điều kiện

Ví dụ: trong biểu đồ dưới đây, tôi muốn tô màu thùng thu nhập trên 100.000 đô la đỏ để hiển thị khách hàng có doanh thu cao.

library(ggplot2) 
library(scales) 

n <- 10000 
cust <- data.frame(cust_id=1:n,cust_rev <- rexp(n,.00001)) 

# I want to use a log scale for my tick marks and bin breaks 
powers <- function(base,exp) sapply(1:exp, function(exp) base^exp) 

ggplot(cust, aes(cust_rev)) + 
    geom_histogram(color="black",fill="light blue", binwidth=1/3) + 
    scale_x_log10(labels=comma, breaks=powers(10,8)) + 
    scale_y_continuous(labels=comma) + 
    xlab("Customer Revenue") + ylab("Number of Customers") + 
    ggtitle("Distribution of Customer Value") 

enter image description here

Ngoài ra, tôi đã cố gắng một workaround với một geom_histogram thứ hai(), nhưng đã không thành công.

ggplot(cust, aes(x=cust_rev)) + 
    geom_histogram(color="black",fill="light blue", binwidth=1/3) + 
    geom_histogram(data=subset(cust,cust_rev>100000), 
       color="black",fill="red", binwidth=1/3) + 
    scale_x_log10(labels=comma, breaks=powers(10,8)) + 
    scale_y_continuous(labels=comma) + 
    xlab("Customer Revenue ($)") + ylab("Number of Customers") + 
    ggtitle("Distribution of Customer Value") 
# Error in data.frame(x = c(45291.1377418786, 52770.7004919648, 15748.975193128, 
# : arguments imply differing number of rows: 10000, 3568 

Trả lời

11

Sẽ dễ nhất là chỉ cần thêm cột khác với điều kiện và cập nhật aes để bao gồm nhóm điền.

cust$high_rev <- as.factor((cust[,2]>100000)*1) 

ggplot(cust, aes(cust_rev, fill=high_rev)) + 
    geom_histogram(color="black", binwidth=1/3) + 
    scale_x_log10(labels=comma, breaks=powers(10,8)) + 
    scale_y_continuous(labels=comma) + 
    xlab("Customer Revenue") + ylab("Number of Customers") + 
    ggtitle("Distribution of Customer Value") 

enter image description here

Nếu bạn đã trái tim của bạn đặt trên một số màu sắc cụ thể mà bạn có thể sử dụng chức năng scale_fill_manual. Dưới đây là một ví dụ với một số màu sắc tươi sáng vui vẻ.

ggplot(cust, aes(cust_rev, fill=high_rev)) + 
    geom_histogram(color="black", binwidth=1/3) + 
    scale_x_log10(labels=comma, breaks=powers(10,8)) + 
    scale_y_continuous(labels=comma) + 
    scale_fill_manual(values = c("green", "purple")) + 
    xlab("Customer Revenue") + ylab("Number of Customers") + 
    ggtitle("Distribution of Customer Value") 

enter image description here

+0

Được chấp nhận cho sử dụng của 'scale_fill_manual()'. Tuy nhiên, như ziggystar (+1) được đề xuất bên dưới, tôi bỏ qua việc thêm một cột vào df và gửi trực tiếp điều kiện đến aes (fill =)). Ngoài ra, tôi đã thêm 'hướng dẫn (fill = FALSE)' vào cốt truyện để ngăn chặn chú giải. – C8H10N4O2

3

Làm thế nào về việc này?

ggplot(cust, aes(cust_rev)) + 
    geom_histogram(aes(fill=cust_rev > 100000),binwidth=1/3) + 
    scale_x_log10() 

hoặc tương đương

ggplot(cust, aes(x=cust_rev,fill=cust_rev > 100000)) + 
    geom_histogram(binwidth=1/3) + 
    scale_x_log10() 
Các vấn đề liên quan