Why it does not showing the year value .in D3plus scatter? - json

I am using D3plus for data visualization . but in the x axis wrong data is showing instead of what i wrote in .x("year") to show .
http://jsfiddle.net/MituVinci/a77kz0dr/
enter code here
var visualization = d3plus.viz()
.container("#viz") // container DIV to hold the visualization
.data(sample_data) // data to use with the visualization
.type("scatter") // visualization type
.id("Reason") // key for which our data is unique on
.x("year") // key for x-axis
.y("Female") // key for y-axis
.draw()
I also want to resize the width and height of this and also want to show it using an external json file how can i do it ?

Since you have "Reason" as your .id() variable, D3plus is aggregating all data points that have the same "Reason". So the "x" position for "Family Feud" is 2010+2011+2012+2013+2014 or 10060, which is where all of your bubbles are located.
If you want to display each bubble individually, you could create a separate variable called "ReasonYear", concat the text of the Reason and Year fields together and then use .id("ReasonYear") for your visualization.
Use .width() and .height() to control the width and height respectively of your visualization.
Use .data() to load data from an external JSON file
Documentation can be found here: https://github.com/alexandersimoes/d3plus/wiki/Visualizations

Related

Altair SchemaValidation Error When Faceting

I have this dataframe:
And I want to make a horizontal stacked bar chart that displays the same information as this chart:
I have generated this code:
chart = alt.Chart(df_q4).mark_bar().encode(
x = alt.X('value:Q', axis=None),
color=alt.Color('color_code:N', scale=None),
).facet("title:N"
).properties(
width=400,
height=700)
chart
However it throws this error:
SchemaValidationError: Invalid specification
altair.vegalite.v4.api.Chart, validating 'required'
'data' is a required property
alt.FacetChart(...)
I am not sure why I cannot facet. I have also tried encoding the facet object within the chart encoding, and that did not work as well.
I also would like to incorporate some code into my chart that shows only the titles, colors, and values pertaining to a specific season. This will be via wrapping the code within a function that's something like:
define color_palette(season):
Facet charts do not support width or height properties. Perhaps you meant this?
chart = alt.Chart(df_q4).mark_bar().encode(
x = alt.X('value:Q', axis=None),
color=alt.Color('color_code:N', scale=None),
).properties(
width=400,
height=700
).facet(
"title:N"
)

Using htmlParse to clean text from dataframe

I have a dataFrame with HTML text in one column and a binary variable in the other column. Looks like this in the first column:
1 <p>I am trying to build a simple map app with Shiny and ggplot2. It works as follow: </p>\n\n<ul>\n<li>user selects a country </li>\n<li>the app loads a shape file and gives a list of input fields for adm1 country regions</li>\n<li>user inputs a numeric value for each region (fields are initially filled with random values) </li>\n<li>all values from input fields are collected in a vector, merged to the map data and given as a <code>fill</code> argument to the <code>ggplot()</code> function</li>\n</ul>\n\n<p>The problem is that ggplot doesn't seem to interpret correctly the input values for each regions. Plus, colors on the map don't change when input values are modified through the UI. I believe the <code>indicator</code> vector fed to the <code>fill</code> argument is not correctly interpreted/passed.</p>\n\n<p>Thank you for your suggestions.</p>\n\n<p><em>Note: in the code below, the shapefiles are sourced on the UCDavis website for reproducibility. I usually store them locally.</... <truncated>
I'm trying to clean, or remove the HTML tags using a for loop, but R is saying this isn't XML code:
for (i in 1:nrow(dataFrame)) {
row <- dataFrame[i,]
htmlParse(dataFrame)
}
Any suggestions?

Hover on <p> element to highlight geographic boundary, D3 Choropleth

I made a choropleth map of Chicago's 77 neighborhoods in D3.
The only challenge is, it's hard to know which neighborhood is which.
So, what I did was make divs with p elements (containing the neighborhood names) in the body of my HTML file and positioned them into a blank spot in my svg/canvas.
See a visual here.
What I'm trying to do is make it so when you hover over the name, the geographic boundary of the neighborhood highlights. Somehow I need to relate the geography to the text, but I have no idea how.
For a more robust solution, you would ideally want to add an id field to your data. This assumes that your data is some format (such as JSON). You may already have a unique identifier that you can use instead, but if not the following should work.
var i = 0;
for (var x in dataSet)
dataSet[x].id = ++i; //Or i++ for zero-based indexing
Now it depends on how you are generating the svg elements, but ideally you are using the enter function of d3. In that case, just create the result of enter as a variable and use it to append both the path (map portion) as well as the text.
var dataSelect = svg.selectAll(".item").data(data.items);
var dataEnter = dataSelect.enter();
dataEnter.append("path").....
dataEnter.append("text").....text(function(d){return d.label;}) //Using text because this is drawn inside the SVG.
With using the data and enter functions, the created objects automatically have the id data bound to them.
This makes it a simple case of text.id == path.id in your mouseover function.
svg.selectAll(".itemText").on("mouseover", function(textItem){
svg.selectAll(".item").each(function (cityItem){
if (cityItem.id == textItem.id)
d3.select(this).style("fill", "green");
else
d3.select(this).style("fill", function(d){return d.color;});
})
});
I've done this in a fiddle which you can see here
Note that this does not use p elements because ideally, if you're using SVG then you probably should use text elements. If you have to use p elements, then you can still use this general technique, but instead using p.text() as the matching factor on mouseover instead of id, assuming that the name is bound to your path data somewhere.

D3.js: How to combine 2 datasets in order to create a map and show values on.mouseover?

I would like to combine two datasets on a map in D3.js.
For example:
1st dataset: spatial data in .json.
2nd dataset: Data to the areas in .csv
--> When you hover on the map a tooltip should show a sentences with some data from the 2nd dataset.
I am able to make the map and show a tooltip with data within the .json-file, but how do I insert the 2nd dataset?
A new function within my function that creates the map?
Do I have to take a completely new way?
Should I merge the .json-file with my 2nd dataset before using d3.js?
I appreciate any thoughts! :)
So, I think what you're asking is how to take spatial data from json and join it with some csv data that is loaded separately?
I did something similar with a choropleth map I was drawing and basically I just created a map of topology element ids to data objects and then I did a lookup using the topology element id to get whatever I wanted to associate with the actual drawn map element (I was using this method to set the color for the choropleth based on the fips country code).
So basically, draw the map so that you have an id associated with each map element that you want to be able to hover over. Then, in your mouseover/mouseout handlers, you will use that id to lookup the data you want to show in the tooltip and either use the svg title element or tipsy or manually draw an svg text element or whatever to show the tooltip.
Here's a couple useful references for drawing tooltips:
https://gist.github.com/biovisualize/1016860
http://jsfiddle.net/reblace/6FkBd/2/
From the fiddle:
function mouseover(d) {
d3.select(this).append("text")
.attr("class", "hover")
.attr('transform', function(d){
return 'translate(5, -10)';
})
.text(d.name + ": " + d.id);
}
// Toggle children on click.
function mouseout(d) {
d3.select(this).select("text.hover").remove();
}
Basically, it's appending an SVG text element and offsetting it from the position of the element being hovered over.
And here's a sample of how I look up data in an external map:
// Update the bound data
data.svg.selectAll("g.map path").transition().duration(750)
.style("fill", function(d) {
// Get the feature data from the mapData using the feature code
var val = mapData[d.properties.code];
// Return the colorScale value for the state's value
return (val !== undefined)? data.settings.colorScale(val) : undefined;
});
If your data is static, you can join it into your topojson file (if that's what you're using). https://github.com/mbostock/topojson/wiki/Command-Line-Reference
The client could change my data, so I kept it separate and redrew the map each time the data changed so that the colors would update. Since my data was topojson, I could access the feature id from the map data using d.properties.code (because I had joined the codes into the topojson file using the topojson tool I reference above... but you could use whatever unique id is in the spatial data file you have).

Imwrite command didn't create an image with true value in Matlab

I'm trying to write .tif image. I wrote the following command either on command window or within function file in Matlab to write tif image with the name of 'A_exam'.
All value of output image (A_exam.tif) are 255, no image displayed, just white screen. A matrix is extracted from following code. When I calculated this code, values of A matrix are correct. The problem just comes from writing an image file.
[~,Ind_max] = max(vgt,[],3);
A= arrayfun(#(y) ...
arrayfun(#(x) emp(x,y, Ind_max(x,y)), 1:size(vgt,1)), ...
1:size(vgt,2), 'UniformOutput',false);
A = reshape([A{:}], size(Ind_max))
imwrite(A,'A_exam.tif','tif')
Thank you
whos A
Name Size Bytes Class Attributes
A 41x41 6724 single
That reshape before calling imwrite looks problematic. I would change the
imwrite(A,'A_exam.tif','tif')
to add a call to mat2gray
imwrite(mat2gray(A),'A_exam.tif');
to make sure you're not mixing matrices and images.