2017-08-31 68 views
1

我有以下的僞數據:更改軸刻度爲「箱」中ggplot

col1 <- c(1:4, 1:4, 1:4, 1:4) 
col2 <- c(168, 2, 40, 12, 2, 60, 16, 3, 70, 20, 15, 11, 200, 150, 1, 100) 
col3 <- c(2:5, 2:5, 2:5, 2:5) 

dummy_data <- cbind(col1, col2, col3) 
dummy_data <- as.data.frame(dummy_data) 


ggplot(dummy_data, aes(col1, col3, size = col2))+ 
    geom_jitter() 

它創建了一個jitterplot其中x軸的刻度是「廣」,在這個意義上,抖動的數據點在劇情中有更大的空間可以覆蓋。

是否有可能創建某種「框」或行將覆蓋此範圍,as in this picture?我希望範圍對觀衆非常清楚。

+0

如果你不介意花括號,你可能想檢查這個問題:https://stackoverflow.com/questions/7001799/ggplot2-curly-braces-on-an-axis – useR

+0

謝謝!很高興能夠從括號和矩形中進行選擇! – Haakonkas

回答

1

試試這個:

ggplot(dummy_data, aes(x = col1))+ 
    geom_jitter(aes(y = col3, size = col2)) + 
    geom_rect(data = data.frame(x = c(1, 2, 3, 4)), 
      aes(xmin = x - 0.5, xmax = x + 0.5, ymin = 1, ymax = 1.5), 
      col = "black", fill = NA, inherit.aes = F) 

ggplot

您可以通過 「ymin的」 & 「YMAX」 向上/向下移動矩形。

+0

謝謝!這工作完美。 – Haakonkas