Skip to content

Instantly share code, notes, and snippets.

@nikhiltitus
Last active February 18, 2018 21:31
Show Gist options
  • Save nikhiltitus/2a3a92b9c82eb76de8896ba3b935146a to your computer and use it in GitHub Desktop.
Save nikhiltitus/2a3a92b9c82eb76de8896ba3b935146a to your computer and use it in GitHub Desktop.

590V assignment 2

  1. A scatterplot is a plot that uses cartesian coordinates to display values of a dataset. It is a graph of points which helps us to visualize the relationship between different dimensions(features).
    • The basic algorithm for creating a scatter plot is follows:
    #Reference: Interactive Data Visualization: Foundations, Techniques, and Applications, Second Edition by Daniel A. Keim, Georges G. Grinstein, and Matthew O. Ward
    
    def scatterplot(xDim,yDim,cDim,rDim,rMin,rMax):
        for each record i:
            x = NORMALIZE(i,xDim)
            y = NORMALIZE(i,yDim)
            r = NORMALIZE(i,rDim,rMin,rMax)
            MAPCOLOR(i,cDim)#Maps the value of the dimension of the data point to a color
            DRAW_CIRCLE(x,y,r) 
    
    • We can gain several insights from the data using a scatter plot. We can see the correlation between the different dimensions(features) of the data point. We can formulate several hypothesis from the data, eg: in our data the MPG decreases as horsepower increases. We can also visualize and fit a curve through the data points. Scatterplots also helps us to visualize multiple dimensions in one go using different features like size, colour, shape etc. Several interesting discoveries can also be made by using a scatter plot. We can also use scatter plots to prove and convince some points by showing the patterns in data.
    • Following are some features of the scatter plot that can be controlled:
      • color of the point: This can be used to show categorical features in general. We can assign a color to a category. This can be controlled by choosing any feature and assigning a color to a value.
      • radius/size of the data point: This can be used to show an extra dimension. We can show continuous as well as some categorical data like the number of cylinders. This can be controlled by normalizing the dimension values and mapping it to radius range.
      • shape of the data point: We can use different shapes for different categories of the data.
      • legends: Legends can give additional info about some dimension by providing a quick look up map. They can be derived from the dimension values of a data.
      • Opacity/transparency of a data point: Varying degree of opacity can show different values of the data point.
      • Tooltip/additional info on mouse over: Additional information of the dimension can be provided when mouse is hovered over a point.
      • Drop downs for changing x and y dimension: x and y axis values can be made selectable by providing drop downs.
      • 3D scatter plot: We can add an extra dimension and visualize the scatterplot in 3D space. This gives us one more dimension.
      • Regression/Best fit line: A regression/best fit line can be used to best represent/fit the data. We can use any linear regression technique and regularization for this.
      • Filters: Filters can be used to filter data on a dimension. It can be a check box with values for a dimension or we can provide fine grain controls like greater than/less than.
      • Tick marks can be used to show the values at intermediate points. If we make tick marks go all across the graph it would help the user to identify data points.
      • Highlighting data points on mouse over: We can highlight data points on mouse over to make the data point explicitly stand out in a scatter plot.
      • X and Y axis labels: X and Y axis labels should be provided to understand the dimensions being represented.
      • Selector brushes: We can use a cricular or lasso selector to select a subset of points to compute some metric.
      • Reset button: A reset button can be provided to reset values to default.
      • Segregated clusters: Clusters can be identified with well specified boundaries. A circle can be drawn around clusters.
      • Derived dimensions: Derived dimensions which is derived from based dimensions can be used to represent data. Like velocity= distance/time. We can allow the user to define these derived dimensions.
      • Zoom and pan in a scatter plot: We can enable the users to zoom and pan the data points, thus enabling them to do more fine grained analysis on the data.
  2. A scatterplot at its basic form is a 2D or 3D representation of data. Most of the problems we deal in real life has a lot of dimensions. I am suggesting a scatter plot where we use PCA dimensionality reduction to reduce the scatter plot dimensions and project the data into a 2D or 3D scatter plot. Here we can more easily identify clusters of data. On mouse over we should be able to see the original data point with its constituent dimensions.
  3. I obtained the data from Prof. Curran's video: https://github.com/curran/d3-in-motion/blob/master/data/cars.csv. The data contains car data with the following features: mpg,cylinders,displacement,horsepower,weight,acceleration,year,origin,name. It has 392 data points.
  4. Please refer the scatter plot.
  5. 2 other variables: Color and Radius(size) of the circle can be changed.
  6. Mouse over/Tool tip is enabled.
  7. An interesting trend to visualize is the relationship between miles per gallon and horsepower for the data set. We can see a clear decline in miles per gallon as we increase the horsepower. This suggests that we will have to make a trade off between power and economy of the vehicle while buying one. Datsun 280-zx is an outlier which gives a very good mpg of 32.7 for 132 horsepower. It is also interesting to see that most of the high power vehicles are from USA and all the high power vehicles have 8 cylinders which suggest a V8 engine as expected. I believe that this visualization of the trend between MPG and horsepower is best captured in a scatter plot. This is because scatter plot is one of the best visualization for discovering the relationship between variables. A line graph using the best fit curve to the data is another visualization that can be used.

References:

  1. https://www.manning.com/livevideo/d3-js-in-motion
  2. Interactive Data Visualization: Foundations, Techniques, and Applications, Second Edition by Daniel A. Keim, Georges G. Grinstein, and Matthew O. Ward
  3. Lab 3 590V Files
mpg cylinders displacement horsepower weight acceleration year origin name
18 8 307 130 3504 12 1970 USA chevrolet chevelle malibu
15 8 350 165 3693 11.5 1970 USA buick skylark 320
18 8 318 150 3436 11 1970 USA plymouth satellite
16 8 304 150 3433 12 1970 USA amc rebel sst
17 8 302 140 3449 10.5 1970 USA ford torino
15 8 429 198 4341 10 1970 USA ford galaxie 500
14 8 454 220 4354 9 1970 USA chevrolet impala
14 8 440 215 4312 8.5 1970 USA plymouth fury iii
14 8 455 225 4425 10 1970 USA pontiac catalina
15 8 390 190 3850 8.5 1970 USA amc ambassador dpl
15 8 383 170 3563 10 1970 USA dodge challenger se
14 8 340 160 3609 8 1970 USA plymouth 'cuda 340
15 8 400 150 3761 9.5 1970 USA chevrolet monte carlo
14 8 455 225 3086 10 1970 USA buick estate wagon (sw)
24 4 113 95 2372 15 1970 Japan toyota corona mark ii
22 6 198 95 2833 15.5 1970 USA plymouth duster
18 6 199 97 2774 15.5 1970 USA amc hornet
21 6 200 85 2587 16 1970 USA ford maverick
27 4 97 88 2130 14.5 1970 Japan datsun pl510
26 4 97 46 1835 20.5 1970 Germany volkswagen 1131 deluxe sedan
25 4 110 87 2672 17.5 1970 Germany peugeot 504
24 4 107 90 2430 14.5 1970 Germany audi 100 ls
25 4 104 95 2375 17.5 1970 Germany saab 99e
26 4 121 113 2234 12.5 1970 Germany bmw 2002
21 6 199 90 2648 15 1970 USA amc gremlin
10 8 360 215 4615 14 1970 USA ford f250
10 8 307 200 4376 15 1970 USA chevy c20
11 8 318 210 4382 13.5 1970 USA dodge d200
9 8 304 193 4732 18.5 1970 USA hi 1200d
27 4 97 88 2130 14.5 1971 Japan datsun pl510
28 4 140 90 2264 15.5 1971 USA chevrolet vega 2300
25 4 113 95 2228 14 1971 Japan toyota corona
19 6 232 100 2634 13 1971 USA amc gremlin
16 6 225 105 3439 15.5 1971 USA plymouth satellite custom
17 6 250 100 3329 15.5 1971 USA chevrolet chevelle malibu
19 6 250 88 3302 15.5 1971 USA ford torino 500
18 6 232 100 3288 15.5 1971 USA amc matador
14 8 350 165 4209 12 1971 USA chevrolet impala
14 8 400 175 4464 11.5 1971 USA pontiac catalina brougham
14 8 351 153 4154 13.5 1971 USA ford galaxie 500
14 8 318 150 4096 13 1971 USA plymouth fury iii
12 8 383 180 4955 11.5 1971 USA dodge monaco (sw)
13 8 400 170 4746 12 1971 USA ford country squire (sw)
13 8 400 175 5140 12 1971 USA pontiac safari (sw)
18 6 258 110 2962 13.5 1971 USA amc hornet sportabout (sw)
22 4 140 72 2408 19 1971 USA chevrolet vega (sw)
19 6 250 100 3282 15 1971 USA pontiac firebird
18 6 250 88 3139 14.5 1971 USA ford mustang
23 4 122 86 2220 14 1971 USA mercury capri 2000
28 4 116 90 2123 14 1971 Germany opel 1900
30 4 79 70 2074 19.5 1971 Germany peugeot 304
30 4 88 76 2065 14.5 1971 Germany fiat 124b
31 4 71 65 1773 19 1971 Japan toyota corolla 1200
35 4 72 69 1613 18 1971 Japan datsun 1200
27 4 97 60 1834 19 1971 Germany volkswagen model 111
26 4 91 70 1955 20.5 1971 USA plymouth cricket
24 4 113 95 2278 15.5 1972 Japan toyota corona hardtop
25 4 97.5 80 2126 17 1972 USA dodge colt hardtop
23 4 97 54 2254 23.5 1972 Germany volkswagen type 3
20 4 140 90 2408 19.5 1972 USA chevrolet vega
21 4 122 86 2226 16.5 1972 USA ford pinto runabout
13 8 350 165 4274 12 1972 USA chevrolet impala
14 8 400 175 4385 12 1972 USA pontiac catalina
15 8 318 150 4135 13.5 1972 USA plymouth fury iii
14 8 351 153 4129 13 1972 USA ford galaxie 500
17 8 304 150 3672 11.5 1972 USA amc ambassador sst
11 8 429 208 4633 11 1972 USA mercury marquis
13 8 350 155 4502 13.5 1972 USA buick lesabre custom
12 8 350 160 4456 13.5 1972 USA oldsmobile delta 88 royale
13 8 400 190 4422 12.5 1972 USA chrysler newport royal
19 3 70 97 2330 13.5 1972 Japan mazda rx2 coupe
15 8 304 150 3892 12.5 1972 USA amc matador (sw)
13 8 307 130 4098 14 1972 USA chevrolet chevelle concours (sw)
13 8 302 140 4294 16 1972 USA ford gran torino (sw)
14 8 318 150 4077 14 1972 USA plymouth satellite custom (sw)
18 4 121 112 2933 14.5 1972 Germany volvo 145e (sw)
22 4 121 76 2511 18 1972 Germany volkswagen 411 (sw)
21 4 120 87 2979 19.5 1972 Germany peugeot 504 (sw)
26 4 96 69 2189 18 1972 Germany renault 12 (sw)
22 4 122 86 2395 16 1972 USA ford pinto (sw)
28 4 97 92 2288 17 1972 Japan datsun 510 (sw)
23 4 120 97 2506 14.5 1972 Japan toyouta corona mark ii (sw)
28 4 98 80 2164 15 1972 USA dodge colt (sw)
27 4 97 88 2100 16.5 1972 Japan toyota corolla 1600 (sw)
13 8 350 175 4100 13 1973 USA buick century 350
14 8 304 150 3672 11.5 1973 USA amc matador
13 8 350 145 3988 13 1973 USA chevrolet malibu
14 8 302 137 4042 14.5 1973 USA ford gran torino
15 8 318 150 3777 12.5 1973 USA dodge coronet custom
12 8 429 198 4952 11.5 1973 USA mercury marquis brougham
13 8 400 150 4464 12 1973 USA chevrolet caprice classic
13 8 351 158 4363 13 1973 USA ford ltd
14 8 318 150 4237 14.5 1973 USA plymouth fury gran sedan
13 8 440 215 4735 11 1973 USA chrysler new yorker brougham
12 8 455 225 4951 11 1973 USA buick electra 225 custom
13 8 360 175 3821 11 1973 USA amc ambassador brougham
18 6 225 105 3121 16.5 1973 USA plymouth valiant
16 6 250 100 3278 18 1973 USA chevrolet nova custom
18 6 232 100 2945 16 1973 USA amc hornet
18 6 250 88 3021 16.5 1973 USA ford maverick
23 6 198 95 2904 16 1973 USA plymouth duster
26 4 97 46 1950 21 1973 Germany volkswagen super beetle
11 8 400 150 4997 14 1973 USA chevrolet impala
12 8 400 167 4906 12.5 1973 USA ford country
13 8 360 170 4654 13 1973 USA plymouth custom suburb
12 8 350 180 4499 12.5 1973 USA oldsmobile vista cruiser
18 6 232 100 2789 15 1973 USA amc gremlin
20 4 97 88 2279 19 1973 Japan toyota carina
21 4 140 72 2401 19.5 1973 USA chevrolet vega
22 4 108 94 2379 16.5 1973 Japan datsun 610
18 3 70 90 2124 13.5 1973 Japan maxda rx3
19 4 122 85 2310 18.5 1973 USA ford pinto
21 6 155 107 2472 14 1973 USA mercury capri v6
26 4 98 90 2265 15.5 1973 Germany fiat 124 sport coupe
15 8 350 145 4082 13 1973 USA chevrolet monte carlo s
16 8 400 230 4278 9.5 1973 USA pontiac grand prix
29 4 68 49 1867 19.5 1973 Germany fiat 128
24 4 116 75 2158 15.5 1973 Germany opel manta
20 4 114 91 2582 14 1973 Germany audi 100ls
19 4 121 112 2868 15.5 1973 Germany volvo 144ea
15 8 318 150 3399 11 1973 USA dodge dart custom
24 4 121 110 2660 14 1973 Germany saab 99le
20 6 156 122 2807 13.5 1973 Japan toyota mark ii
11 8 350 180 3664 11 1973 USA oldsmobile omega
20 6 198 95 3102 16.5 1974 USA plymouth duster
19 6 232 100 2901 16 1974 USA amc hornet
15 6 250 100 3336 17 1974 USA chevrolet nova
31 4 79 67 1950 19 1974 Japan datsun b210
26 4 122 80 2451 16.5 1974 USA ford pinto
32 4 71 65 1836 21 1974 Japan toyota corolla 1200
25 4 140 75 2542 17 1974 USA chevrolet vega
16 6 250 100 3781 17 1974 USA chevrolet chevelle malibu classic
16 6 258 110 3632 18 1974 USA amc matador
18 6 225 105 3613 16.5 1974 USA plymouth satellite sebring
16 8 302 140 4141 14 1974 USA ford gran torino
13 8 350 150 4699 14.5 1974 USA buick century luxus (sw)
14 8 318 150 4457 13.5 1974 USA dodge coronet custom (sw)
14 8 302 140 4638 16 1974 USA ford gran torino (sw)
14 8 304 150 4257 15.5 1974 USA amc matador (sw)
29 4 98 83 2219 16.5 1974 Germany audi fox
26 4 79 67 1963 15.5 1974 Germany volkswagen dasher
26 4 97 78 2300 14.5 1974 Germany opel manta
31 4 76 52 1649 16.5 1974 Japan toyota corona
32 4 83 61 2003 19 1974 Japan datsun 710
28 4 90 75 2125 14.5 1974 USA dodge colt
24 4 90 75 2108 15.5 1974 Germany fiat 128
26 4 116 75 2246 14 1974 Germany fiat 124 tc
24 4 120 97 2489 15 1974 Japan honda civic
26 4 108 93 2391 15.5 1974 Japan subaru
31 4 79 67 2000 16 1974 Germany fiat x1.9
19 6 225 95 3264 16 1975 USA plymouth valiant custom
18 6 250 105 3459 16 1975 USA chevrolet nova
15 6 250 72 3432 21 1975 USA mercury monarch
15 6 250 72 3158 19.5 1975 USA ford maverick
16 8 400 170 4668 11.5 1975 USA pontiac catalina
15 8 350 145 4440 14 1975 USA chevrolet bel air
16 8 318 150 4498 14.5 1975 USA plymouth grand fury
14 8 351 148 4657 13.5 1975 USA ford ltd
17 6 231 110 3907 21 1975 USA buick century
16 6 250 105 3897 18.5 1975 USA chevroelt chevelle malibu
15 6 258 110 3730 19 1975 USA amc matador
18 6 225 95 3785 19 1975 USA plymouth fury
21 6 231 110 3039 15 1975 USA buick skyhawk
20 8 262 110 3221 13.5 1975 USA chevrolet monza 2+2
13 8 302 129 3169 12 1975 USA ford mustang ii
29 4 97 75 2171 16 1975 Japan toyota corolla
23 4 140 83 2639 17 1975 USA ford pinto
20 6 232 100 2914 16 1975 USA amc gremlin
23 4 140 78 2592 18.5 1975 USA pontiac astro
24 4 134 96 2702 13.5 1975 Japan toyota corona
25 4 90 71 2223 16.5 1975 Germany volkswagen dasher
24 4 119 97 2545 17 1975 Japan datsun 710
18 6 171 97 2984 14.5 1975 USA ford pinto
29 4 90 70 1937 14 1975 Germany volkswagen rabbit
19 6 232 90 3211 17 1975 USA amc pacer
23 4 115 95 2694 15 1975 Germany audi 100ls
23 4 120 88 2957 17 1975 Germany peugeot 504
22 4 121 98 2945 14.5 1975 Germany volvo 244dl
25 4 121 115 2671 13.5 1975 Germany saab 99le
33 4 91 53 1795 17.5 1975 Japan honda civic cvcc
28 4 107 86 2464 15.5 1976 Germany fiat 131
25 4 116 81 2220 16.9 1976 Germany opel 1900
25 4 140 92 2572 14.9 1976 USA capri ii
26 4 98 79 2255 17.7 1976 USA dodge colt
27 4 101 83 2202 15.3 1976 Germany renault 12tl
17.5 8 305 140 4215 13 1976 USA chevrolet chevelle malibu classic
16 8 318 150 4190 13 1976 USA dodge coronet brougham
15.5 8 304 120 3962 13.9 1976 USA amc matador
14.5 8 351 152 4215 12.8 1976 USA ford gran torino
22 6 225 100 3233 15.4 1976 USA plymouth valiant
22 6 250 105 3353 14.5 1976 USA chevrolet nova
24 6 200 81 3012 17.6 1976 USA ford maverick
22.5 6 232 90 3085 17.6 1976 USA amc hornet
29 4 85 52 2035 22.2 1976 USA chevrolet chevette
24.5 4 98 60 2164 22.1 1976 USA chevrolet woody
29 4 90 70 1937 14.2 1976 Germany vw rabbit
33 4 91 53 1795 17.4 1976 Japan honda civic
20 6 225 100 3651 17.7 1976 USA dodge aspen se
18 6 250 78 3574 21 1976 USA ford granada ghia
18.5 6 250 110 3645 16.2 1976 USA pontiac ventura sj
17.5 6 258 95 3193 17.8 1976 USA amc pacer d/l
29.5 4 97 71 1825 12.2 1976 Germany volkswagen rabbit
32 4 85 70 1990 17 1976 Japan datsun b-210
28 4 97 75 2155 16.4 1976 Japan toyota corolla
26.5 4 140 72 2565 13.6 1976 USA ford pinto
20 4 130 102 3150 15.7 1976 Germany volvo 245
13 8 318 150 3940 13.2 1976 USA plymouth volare premier v8
19 4 120 88 3270 21.9 1976 Germany peugeot 504
19 6 156 108 2930 15.5 1976 Japan toyota mark ii
16.5 6 168 120 3820 16.7 1976 Germany mercedes-benz 280s
16.5 8 350 180 4380 12.1 1976 USA cadillac seville
13 8 350 145 4055 12 1976 USA chevy c10
13 8 302 130 3870 15 1976 USA ford f108
13 8 318 150 3755 14 1976 USA dodge d100
31.5 4 98 68 2045 18.5 1977 Japan honda accord cvcc
30 4 111 80 2155 14.8 1977 USA buick opel isuzu deluxe
36 4 79 58 1825 18.6 1977 Germany renault 5 gtl
25.5 4 122 96 2300 15.5 1977 USA plymouth arrow gs
33.5 4 85 70 1945 16.8 1977 Japan datsun f-10 hatchback
17.5 8 305 145 3880 12.5 1977 USA chevrolet caprice classic
17 8 260 110 4060 19 1977 USA oldsmobile cutlass supreme
15.5 8 318 145 4140 13.7 1977 USA dodge monaco brougham
15 8 302 130 4295 14.9 1977 USA mercury cougar brougham
17.5 6 250 110 3520 16.4 1977 USA chevrolet concours
20.5 6 231 105 3425 16.9 1977 USA buick skylark
19 6 225 100 3630 17.7 1977 USA plymouth volare custom
18.5 6 250 98 3525 19 1977 USA ford granada
16 8 400 180 4220 11.1 1977 USA pontiac grand prix lj
15.5 8 350 170 4165 11.4 1977 USA chevrolet monte carlo landau
15.5 8 400 190 4325 12.2 1977 USA chrysler cordoba
16 8 351 149 4335 14.5 1977 USA ford thunderbird
29 4 97 78 1940 14.5 1977 Germany volkswagen rabbit custom
24.5 4 151 88 2740 16 1977 USA pontiac sunbird coupe
26 4 97 75 2265 18.2 1977 Japan toyota corolla liftback
25.5 4 140 89 2755 15.8 1977 USA ford mustang ii 2+2
30.5 4 98 63 2051 17 1977 USA chevrolet chevette
33.5 4 98 83 2075 15.9 1977 USA dodge colt m/m
30 4 97 67 1985 16.4 1977 Japan subaru dl
30.5 4 97 78 2190 14.1 1977 Germany volkswagen dasher
22 6 146 97 2815 14.5 1977 Japan datsun 810
21.5 4 121 110 2600 12.8 1977 Germany bmw 320i
21.5 3 80 110 2720 13.5 1977 Japan mazda rx-4
43.1 4 90 48 1985 21.5 1978 Germany volkswagen rabbit custom diesel
36.1 4 98 66 1800 14.4 1978 USA ford fiesta
32.8 4 78 52 1985 19.4 1978 Japan mazda glc deluxe
39.4 4 85 70 2070 18.6 1978 Japan datsun b210 gx
36.1 4 91 60 1800 16.4 1978 Japan honda civic cvcc
19.9 8 260 110 3365 15.5 1978 USA oldsmobile cutlass salon brougham
19.4 8 318 140 3735 13.2 1978 USA dodge diplomat
20.2 8 302 139 3570 12.8 1978 USA mercury monarch ghia
19.2 6 231 105 3535 19.2 1978 USA pontiac phoenix lj
20.5 6 200 95 3155 18.2 1978 USA chevrolet malibu
20.2 6 200 85 2965 15.8 1978 USA ford fairmont (auto)
25.1 4 140 88 2720 15.4 1978 USA ford fairmont (man)
20.5 6 225 100 3430 17.2 1978 USA plymouth volare
19.4 6 232 90 3210 17.2 1978 USA amc concord
20.6 6 231 105 3380 15.8 1978 USA buick century special
20.8 6 200 85 3070 16.7 1978 USA mercury zephyr
18.6 6 225 110 3620 18.7 1978 USA dodge aspen
18.1 6 258 120 3410 15.1 1978 USA amc concord d/l
19.2 8 305 145 3425 13.2 1978 USA chevrolet monte carlo landau
17.7 6 231 165 3445 13.4 1978 USA buick regal sport coupe (turbo)
18.1 8 302 139 3205 11.2 1978 USA ford futura
17.5 8 318 140 4080 13.7 1978 USA dodge magnum xe
30 4 98 68 2155 16.5 1978 USA chevrolet chevette
27.5 4 134 95 2560 14.2 1978 Japan toyota corona
27.2 4 119 97 2300 14.7 1978 Japan datsun 510
30.9 4 105 75 2230 14.5 1978 USA dodge omni
21.1 4 134 95 2515 14.8 1978 Japan toyota celica gt liftback
23.2 4 156 105 2745 16.7 1978 USA plymouth sapporo
23.8 4 151 85 2855 17.6 1978 USA oldsmobile starfire sx
23.9 4 119 97 2405 14.9 1978 Japan datsun 200-sx
20.3 5 131 103 2830 15.9 1978 Germany audi 5000
17 6 163 125 3140 13.6 1978 Germany volvo 264gl
21.6 4 121 115 2795 15.7 1978 Germany saab 99gle
16.2 6 163 133 3410 15.8 1978 Germany peugeot 604sl
31.5 4 89 71 1990 14.9 1978 Germany volkswagen scirocco
29.5 4 98 68 2135 16.6 1978 Japan honda accord lx
21.5 6 231 115 3245 15.4 1979 USA pontiac lemans v6
19.8 6 200 85 2990 18.2 1979 USA mercury zephyr 6
22.3 4 140 88 2890 17.3 1979 USA ford fairmont 4
20.2 6 232 90 3265 18.2 1979 USA amc concord dl 6
20.6 6 225 110 3360 16.6 1979 USA dodge aspen 6
17 8 305 130 3840 15.4 1979 USA chevrolet caprice classic
17.6 8 302 129 3725 13.4 1979 USA ford ltd landau
16.5 8 351 138 3955 13.2 1979 USA mercury grand marquis
18.2 8 318 135 3830 15.2 1979 USA dodge st. regis
16.9 8 350 155 4360 14.9 1979 USA buick estate wagon (sw)
15.5 8 351 142 4054 14.3 1979 USA ford country squire (sw)
19.2 8 267 125 3605 15 1979 USA chevrolet malibu classic (sw)
18.5 8 360 150 3940 13 1979 USA chrysler lebaron town @ country (sw)
31.9 4 89 71 1925 14 1979 Germany vw rabbit custom
34.1 4 86 65 1975 15.2 1979 Japan maxda glc deluxe
35.7 4 98 80 1915 14.4 1979 USA dodge colt hatchback custom
27.4 4 121 80 2670 15 1979 USA amc spirit dl
25.4 5 183 77 3530 20.1 1979 Germany mercedes benz 300d
23 8 350 125 3900 17.4 1979 USA cadillac eldorado
27.2 4 141 71 3190 24.8 1979 Germany peugeot 504
23.9 8 260 90 3420 22.2 1979 USA oldsmobile cutlass salon brougham
34.2 4 105 70 2200 13.2 1979 USA plymouth horizon
34.5 4 105 70 2150 14.9 1979 USA plymouth horizon tc3
31.8 4 85 65 2020 19.2 1979 Japan datsun 210
37.3 4 91 69 2130 14.7 1979 Germany fiat strada custom
28.4 4 151 90 2670 16 1979 USA buick skylark limited
28.8 6 173 115 2595 11.3 1979 USA chevrolet citation
26.8 6 173 115 2700 12.9 1979 USA oldsmobile omega brougham
33.5 4 151 90 2556 13.2 1979 USA pontiac phoenix
41.5 4 98 76 2144 14.7 1980 Germany vw rabbit
38.1 4 89 60 1968 18.8 1980 Japan toyota corolla tercel
32.1 4 98 70 2120 15.5 1980 USA chevrolet chevette
37.2 4 86 65 2019 16.4 1980 Japan datsun 310
28 4 151 90 2678 16.5 1980 USA chevrolet citation
26.4 4 140 88 2870 18.1 1980 USA ford fairmont
24.3 4 151 90 3003 20.1 1980 USA amc concord
19.1 6 225 90 3381 18.7 1980 USA dodge aspen
34.3 4 97 78 2188 15.8 1980 Germany audi 4000
29.8 4 134 90 2711 15.5 1980 Japan toyota corona liftback
31.3 4 120 75 2542 17.5 1980 Japan mazda 626
37 4 119 92 2434 15 1980 Japan datsun 510 hatchback
32.2 4 108 75 2265 15.2 1980 Japan toyota corolla
46.6 4 86 65 2110 17.9 1980 Japan mazda glc
27.9 4 156 105 2800 14.4 1980 USA dodge colt
40.8 4 85 65 2110 19.2 1980 Japan datsun 210
44.3 4 90 48 2085 21.7 1980 Germany vw rabbit c (diesel)
43.4 4 90 48 2335 23.7 1980 Germany vw dasher (diesel)
36.4 5 121 67 2950 19.9 1980 Germany audi 5000s (diesel)
30 4 146 67 3250 21.8 1980 Germany mercedes-benz 240d
44.6 4 91 67 1850 13.8 1980 Japan honda civic 1500 gl
33.8 4 97 67 2145 18 1980 Japan subaru dl
29.8 4 89 62 1845 15.3 1980 Germany vokswagen rabbit
32.7 6 168 132 2910 11.4 1980 Japan datsun 280-zx
23.7 3 70 100 2420 12.5 1980 Japan mazda rx-7 gs
35 4 122 88 2500 15.1 1980 Germany triumph tr7 coupe
32.4 4 107 72 2290 17 1980 Japan honda accord
27.2 4 135 84 2490 15.7 1981 USA plymouth reliant
26.6 4 151 84 2635 16.4 1981 USA buick skylark
25.8 4 156 92 2620 14.4 1981 USA dodge aries wagon (sw)
23.5 6 173 110 2725 12.6 1981 USA chevrolet citation
30 4 135 84 2385 12.9 1981 USA plymouth reliant
39.1 4 79 58 1755 16.9 1981 Japan toyota starlet
39 4 86 64 1875 16.4 1981 USA plymouth champ
35.1 4 81 60 1760 16.1 1981 Japan honda civic 1300
32.3 4 97 67 2065 17.8 1981 Japan subaru
37 4 85 65 1975 19.4 1981 Japan datsun 210 mpg
37.7 4 89 62 2050 17.3 1981 Japan toyota tercel
34.1 4 91 68 1985 16 1981 Japan mazda glc 4
34.7 4 105 63 2215 14.9 1981 USA plymouth horizon 4
34.4 4 98 65 2045 16.2 1981 USA ford escort 4w
29.9 4 98 65 2380 20.7 1981 USA ford escort 2h
33 4 105 74 2190 14.2 1981 Germany volkswagen jetta
33.7 4 107 75 2210 14.4 1981 Japan honda prelude
32.4 4 108 75 2350 16.8 1981 Japan toyota corolla
32.9 4 119 100 2615 14.8 1981 Japan datsun 200sx
31.6 4 120 74 2635 18.3 1981 Japan mazda 626
28.1 4 141 80 3230 20.4 1981 Germany peugeot 505s turbo diesel
30.7 6 145 76 3160 19.6 1981 Germany volvo diesel
25.4 6 168 116 2900 12.6 1981 Japan toyota cressida
24.2 6 146 120 2930 13.8 1981 Japan datsun 810 maxima
22.4 6 231 110 3415 15.8 1981 USA buick century
26.6 8 350 105 3725 19 1981 USA oldsmobile cutlass ls
20.2 6 200 88 3060 17.1 1981 USA ford granada gl
17.6 6 225 85 3465 16.6 1981 USA chrysler lebaron salon
28 4 112 88 2605 19.6 1982 USA chevrolet cavalier
27 4 112 88 2640 18.6 1982 USA chevrolet cavalier wagon
34 4 112 88 2395 18 1982 USA chevrolet cavalier 2-door
31 4 112 85 2575 16.2 1982 USA pontiac j2000 se hatchback
29 4 135 84 2525 16 1982 USA dodge aries se
27 4 151 90 2735 18 1982 USA pontiac phoenix
24 4 140 92 2865 16.4 1982 USA ford fairmont futura
36 4 105 74 1980 15.3 1982 Germany volkswagen rabbit l
37 4 91 68 2025 18.2 1982 Japan mazda glc custom l
31 4 91 68 1970 17.6 1982 Japan mazda glc custom
38 4 105 63 2125 14.7 1982 USA plymouth horizon miser
36 4 98 70 2125 17.3 1982 USA mercury lynx l
36 4 120 88 2160 14.5 1982 Japan nissan stanza xe
36 4 107 75 2205 14.5 1982 Japan honda accord
34 4 108 70 2245 16.9 1982 Japan toyota corolla
38 4 91 67 1965 15 1982 Japan honda civic
32 4 91 67 1965 15.7 1982 Japan honda civic (auto)
38 4 91 67 1995 16.2 1982 Japan datsun 310 gx
25 6 181 110 2945 16.4 1982 USA buick century limited
38 6 262 85 3015 17 1982 USA oldsmobile cutlass ciera (diesel)
26 4 156 92 2585 14.5 1982 USA chrysler lebaron medallion
22 6 232 112 2835 14.7 1982 USA ford granada l
32 4 144 96 2665 13.9 1982 Japan toyota celica gt
36 4 135 84 2370 13 1982 USA dodge charger 2.2
27 4 151 90 2950 17.3 1982 USA chevrolet camaro
27 4 140 86 2790 15.6 1982 USA ford mustang gl
44 4 97 52 2130 24.6 1982 Germany vw pickup
32 4 135 84 2295 11.6 1982 USA dodge rampage
28 4 120 79 2625 18.6 1982 USA ford ranger
31 4 119 82 2720 19.4 1982 USA chevy s-10
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.min.js"></script>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<title>Scatter plot</title>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<div id='vis-container'>
<div class='yaxis'>
Y axis dimension: &nbsp;&nbsp;&nbsp;&nbsp;X axis dimension&nbsp;&nbsp;&nbsp;&nbsp;Shape dimension:&nbsp;&nbsp;&nbsp;&nbsp;Color dimension:<br/><span class='yvalue'></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class='xvalue'></span>&nbsp;&nbsp;&nbsp;&nbsp;
<span class='shapevalue'></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class='colorvalue'></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-outline-primary" id="resetcolor">Reset Color</button>
</div>
<svg width='960' height='500'></svg>
</div>
<script src="script.js">
</script>
</body>
</html>
//Configuration of x,y, shape and color.
var selectData = [{ "text": "Horsepower" }, { "text": "Displacement" },
{ "text": "MPG" }, { "text": "Weight" },
{ "text": "Acceleration" },
];
var xselectData = [{ "text": "Weight" }, { "text": "Horsepower" }, { "text": "Displacement" },
{ "text": "MPG" },
{ "text": "Acceleration" },
];
var shapeselectData = [{ "text": "Cylinders" }, { "text": "Weight" }, { "text": "Horsepower" }, { "text": "Displacement" },
{ "text": "MPG" },
{ "text": "Acceleration" },
];
var colorselectData = [{ "text": "origin" }, { "text": "Cylinders" }]
const svg = d3.select('svg')
const xLabel = 'Weight';
const yLabel = 'Horsepower';
const colorLabel = 'Origin';
const height = svg.attr('height');
const width = svg.attr('width');
const shapeDefaultValue = d => d.cylinders;
const xValue = d => d.weight;
const yvalue = d => d.horsepower;
const colorValue = d => d.origin;
//Domain and Range. domain data, range pixel coordinates
const shapeScale = d3.scaleLinear();
const xScale = d3.scaleLinear();
const yScale = d3.scaleLinear();
const colorScale = d3.scaleOrdinal()
.range(d3.schemeCategory10);
//Setting margins for cut off data
const margin = {
left: 100,
right: 300,
top: 20,
bottom: 100
};
//Spread across inner rectangles within margins
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const g = svg.append('g').attr('transform', `translate(${margin.left},${margin.top})`)
//Putting in the axis
const xAxisG = g.append('g').attr('transform', `translate(0,${innerHeight})`)
const yAxisG = g.append('g')
const colorLegendG = g.append('g').attr('transform', `translate(${innerWidth + 60},150)`);
//Axis Labels
xAxisG.append('text').attr('class', 'xaxis-label').text(xLabel).attr('x', innerWidth / 2)
.attr('y', 68)
yAxisG.attr('class', 'yAxis').append('text').attr('class', 'yaxis-label')
.attr('transform', `rotate(-90)`)
.attr('x', -innerHeight / 2)
.attr('y', -55)
.style('text-anchor', 'middle')
.text(yLabel)
colorLegendG.append('text').attr('class', 'caxis-label')
.attr('y', -40)
.attr('x', -10)
.text(colorLabel);
//Drawing axis and color legend
const xAxis = d3.axisBottom().scale(xScale).tickPadding(15).tickSize(-innerHeight);
const yAxis = d3.axisLeft().scale(yScale).ticks(5).tickPadding(15).tickSize(-innerWidth);
var colorLegend = d3
.legendColor()
.scale(colorScale)
.shape('circle')
.shapeRadius(6)
.shapePadding(2)
.on("cellclick", function (type) {
d3.selectAll(".cell")
.style("opacity", 0.1);
d3.select(this)
.style("opacity", 1);
d3.selectAll(".data-circle")
.style("opacity", 0)
.filter(function (d) {
return d["origin"] == type;
})
.style("opacity", 1)
});
const row = d => {
d.horsepower = +d.horsepower;
d.weight = +d.weight;
d.mpg = +d.mpg;
d.acceleration = +d.acceleration;
d.displacement = +d.displacement;
d.cylinders = +d.cylinders;
return d;
};
//Tool tip for mouse over
var tooltip = d3.select('#vis-container')
.append('div').attr('class', 'tooltip')
.style('opacity', 0)
//Tooltip handler
var tipMouseover = function (d) {
var chtml = ` <b>Name</b>: ${d.name}<br/>
<b>Weight</b>: ${d.weight}<br/>
<b>Origin</b>: ${d.origin}<br/>
<b>Cylinders</b>: ${d.cylinders}<br/>
<b>MPG</b>: ${d.mpg}<br/>
<b>Displacement</b>: ${d.displacement}<br/>
<b>Acceleration</b>: ${d.acceleration}<br/>
<b>Horsepower</b>: ${d.horsepower}<br/>`;
tooltip.html(chtml)
.style("left", (d3.event.pageX + 15) + "px")
.style("top", (d3.event.pageY - 28) + "px")
.transition()
.duration(200)
.style("opacity", .9)
};
// tooltip mouseout event handler
var tipMouseout = function (d) {
tooltip.transition()
.duration(300) // ms
.style("opacity", 0);
};
//Reset color handler
$('#resetcolor').click(function () {
resetColors();
});
//Utility function for adding a selector
function addSelector(selector, change, data) {
var yinput = d3.select(selector)
.append('select')
.attr('id', 'xselect').on('change', change)
.selectAll('option')
.data(data)
.enter().append('option')
.attr('value', function (d) { return d.text })
.text(function (d) { return d.text; })
}
//Utility function for resetting colors
function resetColors() {
d3.selectAll(".data-circle").style('opacity', 1);
d3.selectAll(".cell").style('opacity', 1);
}
//Read CSV and execute call back
d3.csv('cars.csv', row, data => {
//Setting scales
shapeScale.domain(d3.extent(data, shapeDefaultValue))
.range([2, 7]).nice();
xScale.domain(d3.extent(data, xValue))
.range([0, innerWidth]).nice();
yScale.domain(d3.extent(data, yvalue))
.range([innerHeight, 0]).nice();
colorScale.domain(data.map(colorValue))
//Draw the circles of the scatter plot
g.selectAll('circle')
.data(data).enter().append('circle')
.attr('class', 'data-circle')
.attr('cx', d => xScale(xValue(d)))
.attr('cy', d => yScale(yvalue(d)))
.attr('r', d => shapeScale(shapeDefaultValue(d)))
.attr('fill', d => colorScale(colorValue(d)))
.on('mouseover', tipMouseover)
.on('mouseout', tipMouseout);
xAxisG.call(xAxis);
yAxisG.call(yAxis);
colorLegendG.call(colorLegend)
.selectAll('.cell text')
.attr('dy', '0em');
//Adding the y axis selector
addSelector('.yvalue', yChange, selectData);
//Adding x axis selector
addSelector('.xvalue', xChange, xselectData);
//Adding shape axis selector
addSelector('.shapevalue', shapeChange, shapeselectData);
//Adding color axis selector
addSelector('.colorvalue', colorChange, colorselectData);
//Function to populate new y values and y axis values for changes in dimension
function yChange() {
var value = this.value.toLowerCase();
yScale.domain(d3.extent(data, function (d) { return d[value] }))
.range([innerHeight, 0]).nice();
yAxis.scale(yScale);
yAxisG.call(yAxis);
d3.select('.yaxis-label')
.text(value);
d3.selectAll('.data-circle')
.attr('cy', function (d) {
// console.log(d)
return yScale(d[value])
});
}
//Function to populate new x values and x axis values for changes in dimension
function xChange() {
var value = this.value.toLowerCase();
xScale.domain(d3.extent(data, function (d) { return d[value] }))
.range([0, innerWidth]).nice();
xAxis.scale(xScale);
xAxisG.call(xAxis);
d3.select('.xaxis-label')
.text(value);
d3.selectAll('.data-circle')
.attr('cx', function (d) {
// console.log(d)
return xScale(d[value])
});
}
//Function to populate new values for changes in shape
function shapeChange() {
var value = this.value.toLowerCase();
shapeScale.domain(d3.extent(data, function (d) { return d[value] }))
.range([2, 7]).nice();
d3.selectAll('.data-circle')
.attr('r', function (d) {
// console.log(d)
return shapeScale(d[value])
});
}
//Function to populate new color legend
function colorChange() {
resetColors();
var value = this.value.toLowerCase();
d3.select('.caxis-label')
.text(value);
colorScale.domain(data.map(d => d[value]));
// colorScale.domain(data.map(colorValue));
d3.selectAll('.data-circle').attr('fill', function (d) {
return colorScale(d[value]);
});
d3.selectAll('.legendCells').remove();
var newColorLegend = d3
.legendColor()
.scale(colorScale)
.shape('circle')
.shapeRadius(6)
.shapePadding(2)
.on("cellclick", function (type) {
d3.selectAll(".cell")
.style("opacity", 0.1);
d3.select(this)
.style("opacity", 1);
d3.selectAll(".data-circle")
.style("opacity", 0)
.filter(function (d) {
// console.log(value);
return d[value] == type;
})
.style("opacity", 1)
});
colorLegendG.call(newColorLegend)
.selectAll('.cell text')
.attr('dy', '0em');
}
});
.tick .leged-cells{
font-size: 2em;
}
.tick text{
font-size: 2em
}
.axis-label, .xaxis-label,.yaxis-label,.caxis-label{
font-size: 30pt;
fill: black;
font-family: sans-serif;
}
body{
margin-left:20px;
margin-right:20px;
}
.tooltip {
position: absolute;
font-size: 16px;
width: auto;
height: auto;
pointer-events: none;
background-color: white;
}
.yaxis{
position: relative;
left: 92px;
padding-bottom: 10px
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment