1

I have data that looks like this:

A 2 3 LOGIC:A
B 3  3 LOGIC:B
C 2 2 COMBO:A

plot(Data$V2[Data$V4 == "LOGIC:A"], DATA$V3[Data$V4 == "LOGIC:A"])

However I want to plot whenever the column 4 is LOGIC, when I provide "LOGIC" inside the plot command it should plot both "LOGIC:A" and "LOGIC:B". Right now it only accepts the exact column 4 value. Can I use wildcards?

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
identical123456
  • 325
  • 3
  • 12

2 Answers2

3

You can use grepl to find occurrences of your string.

x <- c("LOGIC: A", "COMBO: B")
x[grepl("LOGIC", x)]
[1] "LOGIC: A"
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

Using Data shown reproducibly in the Note at the end this will plot those rows for which V4 contains the substring LOGIC using the character after the colon to represent the point. If you want all points to be represented by the same character omit the pch argument from plot.

plot(V3 ~ V2, Data, subset = grep("LOGIC", V4),  pch = sub("LOGIC:", "", V4))

screenshot

Note

Lines <- "A 2 3 LOGIC:A
B 3  3 LOGIC:B
C 2 2 COMBO:A"
Data <- read.table(text = Lines, as.is = TRUE, strip.white = TRUE)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks. How do I also plot columns 2 vs 3 for all unique Column 4 and return a legend which colorizes based on what we have in column 4? for example all LOGIC:A occurences are colored blue , all COMBO:A occurences are plotted red etc.. Is this possible for R to do it automatically? Say we don't know how many unique column 4 keys we have – identical123456 Sep 17 '18 at 02:41
  • Best to switch to ggplot2 to get automatic legends. `library(ggplot2); ggplot(Data, aes(V2, V3, col = V4)) + geom_point()` – G. Grothendieck Sep 17 '18 at 02:52
  • Thanks for the help. possibly a final question. Can you plot a 45 degree like abline(0,1) in R? And what exactly does the geom_point command do – identical123456 Sep 17 '18 at 03:11
  • https://stackoverflow.com/questions/8693558/how-to-define-fixed-aspect-ratio-for-scatter-plot – G. Grothendieck Sep 17 '18 at 11:31