Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This is my data

mydata = {{0.55, 31.7, 26.360, "acid 0-TOLUIC"}, {0.3925, 31.503, 
31.4861, "acid 2- methyloctanoic"}, {0.711, 11.299, 17.4735, 
"acid 6-hydroxyhexanoic"}, {0.495, 27.57, 30.437, 
"acid acetic"}, {0.506, 49.6, 35.486, 
"acid acetoxyacetic"}, {0.753, 17.37, 13.3028, 
"acid acetylsalicylic"}, {0.873, 5.576, 5.325, 
"acid azelaic"}, {0.652, 17.968, 17.040, 
"acid cyclopentylacetic"}, {0.471, 33.9659, 33.384, 
"acid glutaric"}, {0.753, 26.96, 20.881, 
"acid isophtalic"}, {0.673, 22.433, 26.411, 
"acid maleic"}, {0.627, 25.7, 21.504, "acid m-toluic"}, {0.749, 
12.187, 10.113, "acid neohexanoic"}, {0.5512, 20.3642, 22.555, 
"acid n-nonanoic"}, {0.802, 7.629, 8.243, 
"acid n-undecanoic"}, {0.830, 9.426, 10.141, 
"acid phtalic"}, {0.864, 8.713, 7.9158, "acid salycilic"}};

I want to plot the data with different colors depending to name of the data. So, if the data are (e.g. "acid n-undecanoic" or "acid salycilic") in a particular way I want them in red otherwise in green. Following my not working code.

ListPlot[{mydata[[All, 2]], mydata[[All, 1]]}\[Transpose], 
PlotStyle -> {If[
mydata[[All, -1]] == "acid n-undecanoic" || 
mydata[[All, -1]] == "acid salycilic", Red, Green]}]
share|improve this question

4 Answers 4

ListPlot[List /@ mydata[[;; , {2, 1}]], BaseStyle -> [email protected], PlotRange -> Full,
 PlotStyle -> Replace[mydata[[;; , -1]], 
                      {"acid n-undecanoic" | "acid salycilic" -> Red, _ -> Green},
                      {1}]
 ]
share|improve this answer
    
So it is just plotting not one set of points but many 1-point sets. –  Kuba 2 days ago
    
It works! Thanks a lot... –  Mary 2 days ago

Here is a simple step-by-step approach.

sorted = Sort @ mydata;
pts = List /@ sorted[[All, ;; 2]];
colors = 
  If[StringMatchQ[#, "acid n-undecanoic" | "acid salycilic"], Red, Green] & /@ 
    sorted[[All, -1]];
ListPlot[pts, PlotStyle -> colors]

plot

Update

The above plot would really be more useful if it had tooltips identifying which acid a point represented on mouse-over. Such tooltips are really easy to add. Here is the code showing how to add them:

Module[{sorted, pts, names, colors},
  sorted = Sort @ myData;
  pts = List /@ sorted[[All, ;; 2]];
  names = sorted[[All, -1]];
  colors = 
    If[StringMatchQ[#, "acid n-undecanoic" | "acid salycilic"], Red, Green] & /@ names;
  ListPlot[Thread[Tooltip[pts, names]], PlotStyle -> colors]]

tooltips

share|improve this answer
    
Very smart solution...Thanks a lot! –  Mary 2 days ago

An alternative using Graphics

acids[data_, choice_] := Module[{points},

  points =
   data /. {a_, b_, c_, d_} :>
     If[Or @@ choice /. s_String :> d == s,
      {Text[d, {b, a - 0.025}], {Red, Point[{b, a}]}},
      {Blue, Point[{b, a}]}];

  Graphics[{PointSize[0.02], points},
   AspectRatio -> 1/GoldenRatio,
   Axes -> True,
   AxesOrigin -> {0, 0.3},
   ImageSize -> 400]]

acids[mydata, {"acid glutaric", "acid acetic", "acid azelaic"}]

enter image description here

share|improve this answer

An alternative approach is Styleing individual points in the data set and using the styled data set as the input to ListPlot. Define a coloring function:

colorF["acid n-undecanoic" | "acid salycilic"] = Red;
colorF[_] := Green;

styleddata = Style[{#2, #1}, colorF[#4], [email protected]] & @@@ mydata;
ListPlot[styleddata]

enter image description here

Note: We can also use colorF to set the option value for PlotStyle after breaking mydata into Length[mydata] data sets as in Kuba's approach:

ListPlot[List /@ mydata[[All, {2, 1}]], 
         BaseStyle -> [email protected], 
         PlotStyle -> (colorF /@ mydata[[All, -1]])]

enter image description here

Since we do not break the data into individual points or change the ordering of points in the data set while styling, we can use the option Joined -> True directly, when we need to, without additional processing:

ListPlot[styleddata, Joined->True, PlotMarkers->{Automatic, 0}]

enter image description here

Update: A function similar to colorF can be used for labeling select points (Thanks @eldo for raising the bar:)

labelF[a : "acid n-undecanoic" | "acid salycilic"] := a;
labelF[_] := "";
styleddata = Style[Labeled[{#2, #1}, labelF@#4, Below], colorF@#4, [email protected]] & @@@ mydata;
ListPlot[styleddata]

enter image description here

share|improve this answer
    
That's exceptional! –  Mary 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.